剑指 Offer 06. 从尾到头打印链表

AC代码:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public int[] reversePrint(ListNode head) {
int count = 0;
ListNode temp = head;
while(temp != null) {
temp = temp.next;
count++;
}
int[] res = new int[count];
for(int i=count-1;i>=0;i--) {
res[i] = head.val;
head = head.next;
}
return res;
}
}

浙公网安备 33010602011771号