Java 代码
// 思路:用栈实现
class Solution {
public int[] printListReversingly(ListNode head) {
Stack<Integer> stack = new Stack<Integer>();
ListNode cur = head;
while(cur != null){
stack.push(cur.val);
cur = cur.next;
}
int[] ans = new int[stack.size()];
int i = 0;
while(stack.size() > 0){
ans[i++] = stack.pop();
}
return ans;
}
}