题解 | #从尾到头打印链表#
从尾到头打印链表
http://www.nowcoder.com/practice/d0267f7f55b3412ba93bd35cfa8e8035
/*function ListNode(x){
this.val = x;
this.next = null;
}*/
function printListFromTailToHead(head)
{
let res = []
while(head) {
res.unshift(head.val)
head = head.next
}
return res
}
module.exports = {
printListFromTailToHead : printListFromTailToHead
};
这道题相对来说比较简单,利用while循环来进行当前节点是否为空的判断。为空就不取,非空则增加到res的头部。