首页 > 试题广场 >

从尾到头打印链表

[编程题]从尾到头打印链表
  • 热度指数:1694578 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
输入一个链表的头节点,按链表从尾到头的顺序返回每个节点的值(用数组返回)。

输入{1,2,3}的链表如下图:
返回一个数组为[3,2,1]

0 <= 链表长度 <= 10000
示例1

输入

{1,2,3}

输出

[3,2,1]
示例2

输入

{67,0,24,58}

输出

[58,24,0,67]

说明:本题目包含复杂数据结构ListNode,点此查看相关信息
推荐
方法一:链表从尾到头输出,利用递归实现,不使用库函数直接printf输出的时候用递归比较好
/**
*  struct ListNode {
*        int val;
*        struct ListNode *next;
*        ListNode(int x) :
*              val(x), next(NULL) {
*        }
*  };
*/
class Solution {
public:
    vector<int> printListFromTailToHead(struct ListNode* head) {
        vector<int> value;
        if(head != NULL)
        {
            value.insert(value.begin(),head->val);
            if(head->next != NULL)
            {
                vector<int> tempVec = printListFromTailToHead(head->next);
                if(tempVec.size()>0)
                value.insert(value.begin(),tempVec.begin(),tempVec.end());  
            }         
            
        }
        return value;
    }
};
方法二:用库函数,每次扫描一个节点,将该结点数据存入vector中,如果该节点有下一节点,将下一节点数据直接插入vector最前面,直至遍历完,或者直接加在最后,最后调用reverse
/**
*  struct ListNode {
*        int val;
*        struct ListNode *next;
*        ListNode(int x) :
*              val(x), next(NULL) {
*        }
*  };
*/
class Solution {
public:
    vector<int> printListFromTailToHead(struct ListNode* head) {
        vector<int> value;
        if(head != NULL)
        {
            value.insert(value.begin(),head->val);
            while(head->next != NULL)
            {
                value.insert(value.begin(),head->next->val);
                head = head->next;
            }         
            
        }
        return value;
    }
};

编辑于 2015-06-18 16:53:34 回复(73)
function printListFromTailToHead(head)
{
    let result = []
    let headLength = (Object.keys(head)).length
    if(!headLength);
    else if (!head.next); 
    else printListFromTailToHead(head.next)
    if(headLength)result.push(head.val)
    console.log(result)
    // write code here
}
module.exports = {
    printListFromTailToHead : printListFromTailToHead
};
发表于 2022-06-06 10:20:16 回复(0)
/*function ListNode(x){
    this.val = x;
    this.next = null;
}*/
function printListFromTailToHead(head)
{
    // write code here
    let array1 = [];
    while(head) {
        // 将获取的值push进新数组
        array1.push(head.val);
        // 更新head值
        head = head.next
    }
    // 使用数组的反转方法
    return array1.reverse()
}
module.exports = {
    printListFromTailToHead : printListFromTailToHead
};
发表于 2022-05-14 20:21:54 回复(0)
function printListFromTailToHead(head)
{
    // write code here
    let arr=[]
    let cur=head;
    while(cur){
        arr.push(cur.val)
        cur=cur.next
    }
    return arr.reverse()
}

发表于 2022-05-12 16:26:30 回复(0)
头结点head里居然是有值的吗......
发表于 2022-03-14 11:57:57 回复(0)
function printListFromTailToHead(head)
{
    // write code here
    if (!head) return [];
    return [...printListFromTailToHead(head.next), head.val];
}

发表于 2022-03-08 20:50:03 回复(0)
function printListFromTailToHead(head)
{
    // write code here
    var ArrayList = [];
    if(head){
        var cur = head;
        while(cur){
            ArrayList.unshift(cur.val);
            cur = cur.next;
        }
    }
    return ArrayList;
}
直接用了js的unshift函数~

发表于 2021-03-22 16:45:34 回复(0)
使用JavaScript实现:
参数head指向头结点,val是结点的值,head.next指向下一个结点
function printListFromTailToHead(head)
{
    let result = [];
    while(head) {
        result.unshift(head.val);
        head = head.next;
    }
    return result;
}
编辑于 2021-03-20 20:46:31 回复(0)
var arr = [];
function printListFromTailToHead(head){
     if(head.next === null){
         arr.push(head.val);
     }else{
        printListFromTailToHead(head.next).push(head.val);      
    }
    return arr;
}
老哥们,vscode里面测试正常,这里为啥不行???
发表于 2021-01-26 13:28:39 回复(0)
js版本
function printListFromTailToHead (head) {
  let arr = [];
  while (head != null) {
    arr.unshift(head.val)
    head = head.next;
  }
  return arr;
}

//TS 递归版
export function printListFromTailToHead(head: ListNode,res = []): number[] {
    if(head){
        printListFromTailToHead(head.next,res)
        res.push(head.val)
    }
    return res
}
编辑于 2021-02-24 23:37:46 回复(0)
使用数组方法unshift()从头插入元素,实现从头到尾打印链表。
 /*function ListNode(x){
    this.val = x;
    this.next = null;
}*/
function printListFromTailToHead(head)
{
    // write code here
    let arr = [];
    let node = head;
    while(node){
        arr.unshift(node.val);
        node = node.next;
    }
    return arr;
}


发表于 2021-01-09 09:16:49 回复(0)
function printListFromTailToHead(head){
    if (!head) return [];
    else if(!head.next) return [head.val]
    else return [...printListFromTailToHead(head.next), head.val]
}
发表于 2020-12-26 17:22:57 回复(0)
想问一下大家为什么我这样写不行呢,测试用例通过了,但是一直报返回非零的错误,是因为忽略了什么原因吗
function printListFromTailToHead(head)
{
    // write code here
    let arr = new Array();
    let cur = head;
    while(cur.next){
        arr.push(cur.val);
        cur = cur.next;
    }
    arr.push(cur.val);
    return arr.reverse();
}

发表于 2020-10-28 16:22:15 回复(1)
function printListFromTailToHead(head)
{
    // write code here
    let curr = head,
        arr = []
    while(curr !== null) {
        arr.unshift(curr.val)
        curr = curr.next
    }
    return arr
}
发表于 2020-10-18 23:13:15 回复(0)
function printListFromTailToHead(head)
{
    // write code here
    var res = [];
    while(head){
        res.unshift(head.val);
        head = head.next;
    }
    return res;
}


编辑于 2020-09-13 13:34:08 回复(0)
function printListFromTailToHead(head)
{
    // write code here
    let array = [];
    while(head != null){
        array.unshift(head.val);
        head = head.next;
    }
    return array;
}
利用unshift从头插入数据


编辑于 2020-09-09 13:17:27 回复(0)
JavaScript的 Array 有个方法叫 unshift() ,用于向当前数组前面添加一个或多个元素并返回添加之后数组长度。这一题的链表按从头到尾就行,每得到一个值就按上面这个方法往数组前面添加元素,最后数组的值就是翻转过来的
function printListFromTailToHead(head)
{
    // write code here
    let p = head, arry = [];
    while(p) {
          arry.unshift(p.val);
        p = p.next;
    }
    return arry;
}

发表于 2020-08-27 15:48:19 回复(0)
1.递归搜寻到最后的非空节点
2.push最后节点到答案数组
3.【循环】从内到外解开套娃,返回上层递归push到数组
/*function ListNode(x){
    this.val = x;
    this.next = null;
}*/

function printListFromTailToHead(head) {
    var ansArrList = [];
    function reCur(head) {
        if (head != null) {
            reCur(head.next);
            ansArrList.push(head.val);
        }
        return ansArrList
    }
    return reCur(head)
}


编辑于 2020-03-03 10:50:46 回复(0)
/*function ListNode(x){
    this.val = x;
    this.next = null;
}*/

/**
 * 我的解题思路:
 * 先生成正序的数组,然后再逆序一下就好了
 *
 * @param {*} head 
 */
function printListFromTailToHead(head)
{
    // write code here
    const result = [];
    let nextNode = head;
    while (nextNode) {
        result.push(nextNode.val);
        nextNode = nextNode.next;
    }
    return result.reverse();
}

/**
 * 社区TOP1解答思路:
 * 使用递归来解决时间复杂度的问题,一行搞定
 *
 * @param {*} head 
 */
function topPrintListFromTailToHead(head)
{
    // write code here
    return head ? topPrintListFromTailToHead(head.next).length ? [...topPrintListFromTailToHead(head.next), head.val] : [head.val] : [];
}

发表于 2020-02-14 21:56:34 回复(0)
function printListFromTailToHead(head)
{
    var list=[];
    while(head!=null){
        list.unshift(head.val);
        head=head.next;
    }
    return list;
}

发表于 2019-11-22 19:32:42 回复(0)