简体   繁体   中英

how to append a node to a linked list (python):

Why do I get an error(AttributeError: 'NoneType' object has no attribute 'next') if I substitute inside my while loop "last.next" with "last".Thank you for your help

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.

I think that you can't just use a NoneType object, here the 'last.next', and asign to it a value. You have to define: last = new_node instead of lasst.next = new_node.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM