[英]how to append a node to a linked list (python):
如果我在 while 循環中將“last.next”替換為“last”,為什么會出現錯誤(AttributeError:'NoneType'object 沒有屬性'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.
我認為您不能只使用 NoneType object,這里是“last.next”,並為其分配一個值。 您必須定義:last = new_node 而不是 lasst.next = new_node。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.