Skip to content

Latest commit

 

History

History
29 lines (22 loc) · 585 Bytes

06. 从尾到头打印链表.md

File metadata and controls

29 lines (22 loc) · 585 Bytes

题目链接:

剑指 Offer 06. 从尾到头打印链表

思路:

使用一个辅助栈,遍历链表时,先将元素放入辅助栈。

最后从尾遍历辅助栈。

代码:

JavaScript

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;
};