剑指06 从尾到头打印链表
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public int[] reversePrint(ListNode head) {
Stack<Integer> stack = new Stack<Integer>();
ListNode p = head;
while (head != null) {
stack.push(head.val);
head = head.next;
}
int[] ans = new int[stack.size()];
for (int i = 0; i < ans.length; i++) {
ans[i] = stack.peek();
stack.pop();
}
return ans;
}
}