題目描述
輸入一個鏈表,從尾到頭打印鏈表每個節點的值。
這裏採用棧進行輔助反向打印`
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
Stack<Integer> stack = new Stack<>();
while (listNode != null) {
stack.push(listNode.val);
listNode = listNode.next;
}
ArrayList<Integer> arrayList = new ArrayList<>();
while (!stack.isEmpty())
arrayList.add(stack.pop());
return arrayList;
}
“`