剑指 Offer 06. 从尾到头打印链表
class Solution {
public int[] reversePrint(ListNode head) {
ListNode tmp = head;
int len = 0;
while(tmp!=null){
++len;
tmp = tmp.next;
}
int[] res = new int[len];
tmp = head;
int index = len-1;
while(tmp!=null){
res[index--] = tmp.val;
tmp = tmp.next;
}
return res;
}
}
1.找到长度
2.再查一遍给出结果
时间复杂度O(N)
空间复杂度O(N)