hey, not sure if anyone can help, i get nodes mostly but this one thing i’m confused on. why in this code do we need to make node1 = node1.get_next_node() as in my head, then you havent checked the line of code directly above if it is equal to var1
i also have another node question that i get and then forget why it works if anyone could help there. I’ll have to try and attach a screenshot after
Thanks!
Hi,
while node1 is not None:
means the loop will continue as long as node1 is a valid node
if node1.get_value() == val1:
break
this compares the value contained in node1. If it’s the same as val1 we’ve got to the place we wanted, and ‘break’ exits us out of the loop
next1_prev = node1
ensures we keep track of the previous node in the list before we update the current node
node1 = node1.get_next_node()
moves node1 to the next node in the list. If we’ve got to the end of the list, there wont be a next node and it’ll be set to None - so when it loops back to the while the loop will stop.
The aim is when we’ve finished the loop, either;
- node1 will be a node that has the same value as val1
and node1_prev will be the node just before it in the list
- or, node1 will be None, indicating no node in the list had a value of val1
Hope that helps
1 Like
Thats great, thanks alot!
1 Like