用js刷剑指offer(从尾到头打印链表)

题目描述

输入一个链表,按链表从尾到头的顺序返回一个ArrayList。

牛客网链接

js代码

/*function ListNode(x){
    this.val = x;
    this.next = null;
}*/
function printListFromTailToHead(head)
{
    // write code here
    const res = []
    const stack = []
    if (head === null){
        return res
    }
    while (head !== null){
        stack.push(head.val)
        head = head.next
    }
    while (stack.length !== 0){
        res.push(stack.pop())
    }
    return res
}
posted @ 2019-09-20 11:33  1Shuan  阅读(66)  评论(0编辑  收藏  举报