This is a node question.
Below is the URL and a full answer.

The exact lines that I don’t quite get is the last two lines. Why is the current_node becomes none?
and how could there an “else”? In iteration, after checking current value not to be the value to be removed, is there a possibility that the next node is not “the value to be removed”?

current_node = None
else:
current_node = next_node

``````# 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

# Define your remove_node method below:
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
``````

If you’re wondering what should happen then you shouldn’t be paying too much attention to the code itself.

If you’re wondering how to implement something, then you have to decide what that something is first.

Those are two separate problems. For each problem you’re missing something you should consider first, and you’re combining the two problems into one, making it even more difficult.

Start by considering how one goes about removing a node from a linked list. There would be some kind of rearranging of nodes which you could describe in english. And then you can move on from there.

If you don’t conceptually know how to go about removing a node, then you would in turn consider what a linked list is, and from that you can work out what removing a node means.

If you’re really confused you should try writing the code on paper and leave a column for keeping track of variables and output. Try working out which different outcomes would trigger different conditionals.

But setting current node to none will it not alter the vale of current node?

hope this helps, you should still draw it out as it gets harder after this

``````#function to remove node
def remove_node(self, value_to_remove):
#sets temporary value as the head_node (used to check if it's the one to remove)

# conditional for it being the correct value to remove
if current_node.get_value() == value_to_remove:
#essentially removes the head_node from the list by setting it's neighbord as the new head

#if it wasn't the one you needed to remove
else:

#as long as temp value current_node is it's original stated value
while current_node:
#set's another temporary value, next_node to continue the process of checking
next_node = current_node.get_next_node()
if next_node.get_value() == value_to_remove:
#changes the value to replace
current_node.set_next_node(next_node.get_next_node())
#but you're still a while loop as long as current_node is equal it's original value, so you break it by setting it to None
current_node = None
else:
#this is the last possibility in all the conditionals, note that it also break the while loop
current_node = next_node
``````

are you asking whether assigning a variable to a new value alters the old one?

assignment doesn’t cause operations to happen on the old value, no. nothing at all happens with the old value. assignment points a variable to a new value, that’s the only thing it does

or maybe you’re saying your variable is part of a Node? … is it?

I was asking setting the node to none would change the value it holds.

that’s better because now you’ve eliminated the whole linked-list context, and eliminated the code, and now you’re instead asking what assignment does

however, you can probably answer it yourself.

``````x = []
x = 3  # is the list changed?
``````

x is not the list, it is not part of that list. changing what x points to is not something that affects the list

The last possibility, the last line. Is it the case when there is no value at all in the list? If so, setting current node to none instead of to next value could also be fine, then.

your variable is not part of the data structure.

to modify your data structure you would have to change something that is part of it

1 Like

No, this is very important. It needs to be next_node because you still need the while loop to search the list for what you’re looking for. It would break the loop, but it woldn’t find the actual value you’re looking to remove… and would basically not do its job properly.

This is why @ionatan keeps suggesting you to think about the bigger purpose of the function.

Before you actually try to interpret code, it helps to have a clear understanding of what it aims to do (or a good hypothesis).

In this case, it’s important to have a clear image of how the linked list is supposed to behave. The only reason the remove function is written like this is because of the characteristics the Linked List and Node class have. If it were a different data structure, the remove function is going to be written differently (with the same aim).

I think I see your point now. The last line is to get the loop going when the next_node is not what I am looking for.
I have another question in a stack. Can you help me with this?
Below is the URL to my next question.