首页 > 试题广场 >

从尾到头打印链表

[编程题]从尾到头打印链表
  • 热度指数:1695639 时间限制: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)
直接正向输出到数组中,然后数组逆序就可以了。
class Solution:
    def printListFromTailToHead(self , listNode: ListNode) -> List[int]:
        # write code here
        s = []
        #正序输出链表到栈中
        while listNode:
            s.append(listNode.val)
            listNode = listNode.next
        return s[::-1]


发表于 2022-06-12 16:15:57 回复(0)
class Solution:
    def printListFromTailToHead(self , listNode: ListNode) -> List[int]:
        # write code here
        heap = []
        while listNode:
            heap.append(listNode.val)
            listNode = listNode.next
        return heap[::-1]

发表于 2022-01-22 18:06:59 回复(0)
class Solution:
    # 返回从尾部到头部的列表值序列,例如[1,2,3]
    def printListFromTailToHead(self, l):
        # write code here
        l1 = []
        while l:
            l1.append(l.val)
            l = l.next
        return l1[::-1]
发表于 2021-04-18 08:26:43 回复(0)
class Solution:
    # 返回从尾部到头部的列表值序列,例如[1,2,3]
    def printListFromTailToHead(self, listNode):
        # write code here
        if not listNode:
            return []
        stack = []
        stack.append(listNode.val)
        while listNode.next:
            listNode = listNode.next
            stack.append(listNode.val)
        return stack[::-1]
发表于 2021-03-29 14:36:01 回复(0)

python解法:
解法1:把链表数据存入列表,之后反转链表;

    def printListFromTailToHead(self, listNode):
        if not listNode:
            return []
        res = []
        while listNode:
            res.append(listNode.val)
            listNode = listNode.next
        return res[::-1]

解法2:递归;

class Solution:
    # 返回从尾部到头部的列表值序列,例如[1,2,3]
    def __init__(self):
        self.res = []
    def printListFromTailToHead(self, listNode):
        if not listNode:
            return self.res
        res = self.printListFromTailToHead(listNode.next)
        res.append(listNode.val)
        return res

解法3:逆置链表再存入列表;

class Solution:
    # 返回从尾部到头部的列表值序列,例如[1,2,3]
    def printListFromTailToHead(self, listNode):
        if not listNode:
            return []
        p = ListNode(0)
        p.next = None
        r = p
        res = []
        while listNode:
            q = listNode.next # 暂存后继结点;
            listNode.next = p.next
            p.next = listNode # 把结点依次插入到自己创建的结点后面,实现逆置;
            listNode = q
        ptr = r.next
        while ptr:
            res.append(ptr.val)
            ptr = ptr.next
        return res

解法4:逆置链表再存入列表,和解法3的逆置方法不同;

class Solution:
    # 返回从尾部到头部的列表值序列,例如[1,2,3]
    def printListFromTailToHead(self, listNode):
        if not listNode:
            return []
        res = []
        p = None
        q = listNode
        while q:
            listNode = listNode.next
            q.next = p
            p = q
            q = listNode
        while p:
            res.append(p.val)
            p = p.next
        return res
发表于 2021-02-24 10:51:33 回复(0)
# -*- coding:utf-8 -*-
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    # 返回从尾部到头部的列表值序列,例如[1,2,3]
    def printListFromTailToHead(self, listNode):
        # write code here
        cur=listNode
        l1=[]
        while cur:
            l1.append(cur.val)
            cur=cur.next
        l1.reverse()
        return l1
发表于 2020-12-23 19:52:10 回复(0)
1、递归的理解,递归就是将大问题拆成小问题
2、考虑临界情况
class Solution:
    # 返回从尾部到头部的列表值序列,例如[1,2,3]
    def printListFromTailToHead(self, listNode):
        # write code here
        if listNode is None:
            return []
        self.result = []
        self.get_after_node(listNode)
        return self.result

    def get_after_node(self, listNode):
        if listNode.next is None:
            self.result.append(listNode.val)
            return
        else:
            self.get_after_node(listNode.next)
            self.result.append(listNode.val)
            return


发表于 2020-09-03 23:53:25 回复(0)
每次弹出的值放在输出列表的第一个位置,指导循环结束
# -*- coding:utf-8 -*-
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    # 返回从尾部到头部的列表值序列,例如[1,2,3]
    def printListFromTailToHead(self, listNode):
        # write code here
        ret = []
        if listNode == None:
            return []
        while listNode:
            ret.insert(0, listNode.val)
            listNode = listNode.next
            
        return ret

发表于 2020-08-29 23:21:11 回复(0)
class Solution:
    # 返回从尾部到头部的列表值序列,例如[1,2,3]
    def printListFromTailToHead(self, listNode):
        # write code here
        array_list=[]
        while listNode:
            array_list.insert(0,listNode.val)
            listNode=listNode.next
        return array_list
发表于 2020-08-19 16:27:06 回复(0)
# -*- coding:utf-8 -*-
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    # 返回从尾部到头部的列表值序列,例如[1,2,3]
    def printListFromTailToHead(self, listNode):
        # write code here
        l = []
        while listNode:
            l.insert(0,listNode.val)
            listNode = listNode.next
        return l
append:只能在列表后方加入新数据;而insert可以在列表任意位置加入新数据。
编辑于 2020-07-29 16:04:06 回复(0)
class Solution:
    # 返回从尾部到头部的列表值序列,例如[1,2,3]
    def printListFromTailToHead(self, listNode):
        if not listNode:
            return []
        list_ = []
        while listNode:
            list_.append(listNode.val)
            listNode = listNode.next
        return list_[::-1]

发表于 2020-07-18 17:39:13 回复(0)
class Solution:
    def printListFromTailToHead(self, listNode):
        self.result = []
        if not listNode:
            return []
        self.printListFromTailToHead(listNode.next)
        self.result.append(listNode.val)
        return self.result

发表于 2020-06-25 20:35:18 回复(0)
小白求解答这个stack为啥报错nonetype呀
发表于 2020-05-13 18:01:27 回复(0)
 if listNode is None:
            return []
        res = []
        while listNode is not None:
            res.append(listNode.val)
            listNode = listNode.next
        res.reverse()
        return res
发表于 2020-03-21 15:43:32 回复(0)
class Solution:
    # 返回从尾部到头部的列表值序列,例如[1,2,3]
    def printListFromTailToHead(self, listNode):
        if not listNode:
            return []
        stack = []
        while listNode:
            stack.append(listNode.val)
            listNode = listNode.next
        return stack[::-1]
使用栈结构
发表于 2020-03-05 19:07:47 回复(0)
递归调用即可。不知道是不是测试程序的原因,类内新建函数老是无法调用。最后直接写成了内嵌函数,看来有点别扭。
class Solution:
    # 返回从尾部到头部的列表值序列,例如[1,2,3]
    def printListFromTailToHead(self, listNode):
        (894)# write code here
        def addElement(listNode,list):
            if listNode.next != None:
                addElement(listNode.next, list)
            list.append(listNode.val)
            return list

                    
        list =[ ]
        if listNode == None:
            return list
        else:
            return addElement(listNode, list)


发表于 2020-02-24 23:16:10 回复(0)
请问下这个代码为什么报错 [object of type 'NoneType' has no len()
tt.reverse()改成tt[::-1]就行, 用例是这个{67,0,24,58}

class Solution:
    # 返回从尾部到头部的列表值序列,例如[1,2,3]
    def printListFromTailToHead(self, listNode):
        # write code here
        if not listNode:
            return []
        
        tt = []
        while listNode:
            tt.append(listNode.val)
            listNode = listNode.next
        #return tt[::-1]
        return tt.reverse()
发表于 2020-02-17 00:21:22 回复(1)
python中内置了insert的方法,insert(index,value)用于向列表中指定的索引位置之前插入新的对象,因为是在对应目标之前插入,而append()方法一样将对象添加到列表末尾
# -*- coding:utf-8 -*-
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    # 返回从尾部到头部的列表值序列,例如[1,2,3]
    def printListFromTailToHead(self, listNode):
        # write code here
        L = []
        head = listNode
        while head:
            L.insert(0,head.val)
            head = head.next
        return L

发表于 2020-02-03 21:33:59 回复(0)