题目

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

思路

栈,先进后出

代码

/* function ListNode(x){ this.val = x; this.next = null; }*/
function printListFromTailToHead(head) {
  // write code here
  const res = [];
  let pNode = head;
  while (pNode !== null) {
    res.unshift(pNode.val);
    pNode = pNode.next;
  }
  return res;
}