1 Star 0 Fork 1.5K

沐瑶 / LearningNotes

forked from 陌溪 / LearningNotes 
Create your Gitee Account
Explore and code with more than 6 million developers,Free private repositories !:)
Sign up
Clone or download
README.md 1.66 KB
Copy Edit Web IDE Raw Blame History
陌溪 authored 2020-04-26 08:29 . add code and blog

从尾到头打印链表

题目描述

来源:https://www.nowcoder.com/practice/d0267f7f55b3412ba93bd35cfa8e8035

输入一个链表,按链表从尾到头的顺序返回一个ArrayList。

思考

在python中定义链表

# 链表结构
class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None

# 打印链表
def printChain(head):
    node = head
    while node:
        print(node.val)
        node = node.next

if __name__ == '__main__':
    # 创建链表
    l1 = ListNode(1)
    l2 = ListNode(2)
    l3 = ListNode(3)
    l1.next = l2
    l2.next = l3
    printChain(l1)
        

下面我们看题目是需要倒序输出,那么我们使用一个数组记录,每次插入到数组的前面

while node:
    print(node.val)
    list.insert(0, node.val)
    node = node.next

完整代码如下

# 从头到尾打印链表
# 输入一个链表,按链表从尾到头的顺序返回一个ArrayList。

# 链表结构
class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None

# 打印链表
def printChain(head):
    node = head
    while node:
        print(node.val)
        node = node.next

class Solution:
    # 返回从尾部到头部的列表值序列,例如[1,2,3]
    def printListFromTailToHead(self, listNode):
        node = listNode
        list = []
        while node:
            print(node.val)
            list.insert(0, node.val)
            node = node.next
        return list
if __name__ == '__main__':
    # 创建链表
    l1 = ListNode(1)
    l2 = ListNode(2)
    l3 = ListNode(3)
    l1.next = l2
    l2.next = l3
    Solution().printListFromTailToHead(l1)

Comment ( 0 )

Sign in for post a comment

1
https://gitee.com/muyao_vip/LearningNotes.git
git@gitee.com:muyao_vip/LearningNotes.git
muyao_vip
LearningNotes
LearningNotes
master

Search

102255 3a0e046c 1850385 102255 7aaa926c 1850385