题目
分析
使用栈的先进后出原则,学习链表的取值,迭代
代码
class Solution {
public int[] reversePrint(ListNode head) {
Stack<Integer> stack = new Stack<Integer>();
ListNode p = head;
while (p!=null) {
stack.add(p.val);
p = p.next;
}
int len = stack.size();
int[] ans = new int[len];
for (int i = 0; i < len; i++) {
ans[i] = stack.pop();
}
return ans;
}
}