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