使用一个辅助栈,遍历链表时,先将元素放入辅助栈。
最后从尾遍历辅助栈。
const reversePrint = head => {
const stack = [];
const res = [];
while (head) {
stack.push(head.val);
head = head.next;
}
const len = stack.length;
for (let i = 0; i < len; i++) {
res.push(stack.pop());
}
return res;
};