输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回)。
重点在于用数组返回,立刻想到 栈 的特点
class Solution {
public int[] reversePrint(ListNode head) {
Stack<Integer> stk = new Stack<>();
while(head!=null){
stk.push(head.val);
head=head.next;
}
int[] ret = new int[stk.size()];
int i=0;
while (!stk.isEmpty()){
ret[i++] = stk.pop();
}
return ret;
}
}