In insertEnd
, you’re guaranteeing you always have None
for actualnode
at the end of the loop:
def insertEnd(self, data):
self.size += 1
newnode = Node(data)
actualnode = self.head
# Keep going until actualnode is None
while actualnode is not None:
actualnode = actualnode.nextnode
# Here actualnode will always be None
actualnode.nextnode = newnode
To fix this, just change your while loop condition:
while actualnode.nextnode is not None:
actualnode = actualnode.nextnode
Note that this assumes self.head
is always non-None
. You should probably explicitly check for this condition. Maybe something like this:
def insertEnd(self, data):
self.size += 1
newnode = Node(data)
if self.head is None:
self.head = newnode
else:
actualnode = self.head
while actualnode.nextnode is not None:
actualnode = actualnode.nextnode
actualnode.nextnode = newnode
solved Getting a none type error python 3?