【问题标题】:how to append a node to a linked list (python):如何将节点附加到链表(python):
【发布时间】:2021-01-13 19:16:56
【问题描述】:

如果我在 while 循环中将“last.next”替换为“last”,为什么会出现错误(AttributeError: 'NoneType' object has no attribute 'next')。谢谢您的帮助

1.
2.
3.    def append_right(self,data):
4.            new_node = Node(data)
5.            last = self.head
6.            if self.head is None:
7.                self.head = new_node
8.                return
9.            while last:
10.                last = last.next
11.            last.next = new_node
12.

【问题讨论】:

  • 当你的while循环退出时,最后一个为None。

标签: python singly-linked-list


【解决方案1】:

我认为你不能只使用 NoneType 对象,这里是 'last.next',然后给它赋值。您必须定义:last = new_node 而不是 last.next = new_node。

【讨论】:

    猜你喜欢
    • 2021-01-01
    • 2017-07-07
    • 1970-01-01
    • 2020-02-07
    • 2015-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多