Pouya Jigsaw
Pouya Jigsaw

Reputation: 13

AttributeError: 'NoneType' object has no attribute 'next_node'

I've been doing some training in data structures and i'm implementing linked list. here's the code:

def insertEnd(self, data):

        if self.head is None:
            self.insertStart(data)
            return

        new_node = Node(data)
        self.counter += 1
        actual_node = self.head

        while actual_node is not None:
            actual_node = actual_node.next_node

        actual_node.next_node = new_node

after i try to insert the second node i get this error. i dont know what's the problem

Upvotes: 0

Views: 3710

Answers (1)

Han Song
Han Song

Reputation: 54

In while condition, you should check next node is None. That means current node is the last node. And the new node should be appended after it.

while actual_node.next_node is not None:

Upvotes: 2

Related Questions