剑指offer 66道-python+JavaScript
从尾单头打印链表
时间限制:1秒 空间限制:32768K 热度指数:858443
题目描述
输入一个链表,按链表值从尾到头的顺序返回一个ArrayList
思路
分别用python和javascript实现
github
python代码链接: https://github.com/seattlegirl/jianzhioffer/blob/master/print-the-list-from-end-to-end.js.
题目代码(python)
function ListNode(x){
this.val = x;
this.next = null;
}
function printListFromTailToHead(head)
{
// write code here
var array=[];
var arr=[];
while(head){
array.push(head.val);
head=head.next;
}
while(array.length){
arr.push(array.pop())
}
return arr;
}
github
JavaScript代码链接: https://github.com/seattlegirl/jianzhioffer/commit/aa92861a5dcb8b01eb73e356aa94db628451e51a.
题目代码(JavaScript)
function ListNode(x){
this.val = x;
this.next = null;
}
function printListFromTailToHead(head)
{
// write code here
var array=[];
var arr=[];
while(head){
array.push(head.val);
head=head.next;
}
while(array.length){
arr.push(array.pop())
}
return arr;
}