题目描述
输入一个链表,从尾到头打印链表每个节点的值。
这里采用栈进行辅助反向打印`
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;
}
“`