题解 | #从尾到头打印链表#
从尾到头打印链表
https://www.nowcoder.com/practice/d0267f7f55b3412ba93bd35cfa8e8035
function printListFromTailToHead(head) { let res = []; while(head){ res.push(head.val) head =head.next } return res.reverse(); } module.exports = { printListFromTailToHead : printListFromTailToHead };
使用while语句从头读链表,当节点不为空时插入,并将节点后移,为空时跳出循环