剑指offer 66道:从尾到头打印链表

本文介绍了一道剑指Offer的编程题,即从尾到头打印链表。提供了Python和JavaScript两种语言的实现代码,通过先遍历链表将节点值存入数组,再反转数组来达到逆序打印的效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

从尾单头打印链表

时间限制: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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值