# Why is this line here?

hello guys!
i’m wondering why is this line here. i fully understand the rest of the code, except for existance of this specific line

``````# We'll be using our Node class
class Node:
def __init__(self, value, next_node=None):
self.value = value
self.next_node = next_node

def get_value(self):
return self.value

def get_next_node(self):
return self.next_node

def set_next_node(self, next_node):
self.next_node = next_node

def __init__(self, value=None):

def insert_beginning(self, new_value):
new_node = Node(new_value)

def stringify_list(self):
string_list = ""
while current_node:
if current_node.get_value() != None:
string_list += str(current_node.get_value()) + "\n"
current_node = current_node.get_next_node()
return string_list

def remove_node(self,value_to_remove):
if current_node.get_value() == value_to_remove:
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
# Define your remove_node method below:
``````

this line

``````         current_node = None
``````

p.s: ah, and here’s the subject’s link.if you’d like to take a look

Are you talking about the remove node function?

Think about the algorithm that’s happening and it’ll be clear why.

In order to remove a node in a linked list,

• we have to do a linear search through the list for the node first
• once the node is found, we have to make sure the node preceding it and the node after have a link. (otherwise, by removing the node we’d split the list, and lose access to the tail, which is not what we want). By updating the link, this virtually loses access to the target node (so we can terminate). In a non-garbage collected programming language we’d have to explicitly delete that node (`del` also exists in python but it’s not required).
• if the node is not found we should terminate once we traverse the entire list.

Notice how I mentioned the 2 criteria for termination, finding the node and removing it, or not finding it after traversing. It seems like a small detail but all algorithms must have defined termination steps to be proper algorithms.

Think about that in terms of the while loop evaluation and you have the answer to your question.

2 Likes