Leetcode:剑指 Offer 06. 从尾到头打印链表
while比for的效率更低
class Solution {
public int[] reversePrint(ListNode head) {
Stack<Integer> stack=new Stack<Integer>();
if(head==null){
return new int[0];
}
int count=0;
while(head!=null){
stack.push(head.val);
head=head.next;
}
int size=stack.size();
int[] res=new int[size];
//while比for的效率更低
for(int i=0;i<size;i++){
res[i]=stack.pop();
}
return res;
}
}

浙公网安备 33010602011771号