题目描述
输入一个链表,从尾到头打印链表每个节点的值。
解:
先遍历,在pop()收集
/**function ListNode(x){
this.val = x;
this.next = null;
}*/
function printListFromTailToHead(head)
{
var stackArray = [];
var returnArray = [];
if(head != null){
stackArray.push(head.val);
}else return [];
while(head.next !== null){
head = head.next;
stackArray.push(head.val);
} ;
while(stackArray.length>0){
returnArray.push(stackArray.pop());
}
return returnArray;
}
module.exports = {
printListFromTailToHead : printListFromTailToHead
};
网友评论