What's the difference between the solution code and my code? (Linked Lists)

Hi! I’ve been going through the computer science course (Linked Lists) in python and couldn’t quite wrap my head around on why my code was different from the code given in the solution.

This is my code:

``````    else:
while current_node:
current_node = current_node.get_next_node()
if current_node.get_value() == value_to_remove:
current_node.set_next_node(current_node.get_next_node())
current_node = None
else:
current_node = current_node.get_next_node()
``````

and here is the solution code:

``````else:
while current_node:
next_node = current_node.get_next_node()
if next_node.get_value() == value_to_remove:
current_node.set_next_node(next_node.get_next_node())
current_node = None
else:
current_node = next_node
``````

Wouldn’t the next_node be equivalent to the current_node if current_node is set to current_node.get_next_node()? The error message that pops up is:

Does your `else` case traverse the list and remove the node with a value of `value_to_remove` ?

Pretend you have a linked list with the following nodes with the format:
node(value, next_node) (labels for convenience)-
a(0, b) # value of 0, next_node is b
b(1, c)
c(2, d)

Let’s say we want to remove the node with a value of 1. So we’re starting from a; in your code we take `current_node` and assign it to the value of the next node (b in this case). We then check if `current_node` (now the node with label of b) has the value of 1. It does.

We then use the `.set_next_node` method to assign the `.next_node` of `current_node` (which is currently b) to the outcome of the `.get_next_node` attribute which is c.

So because we found a value of 1 we take `current_node` (b) and re-assign it’s `.next_node` to the next node (c). But that was already the case so in effect we change nothing.

Consider carefully which node should be updated to remove the node with a value of 1 here and make sure your code reflects that.