FAQ: Linked Lists: Python - Linked List Implementation III

I assume you still have a reference to your head_node somewhere or you’d not be able to access your list at all, self.head_node perhaps? The reference count is for references to the object itself, not what objects that object subsequently refers to or you’d never be able to remove anything. So if b points to c but a does not point to b (and nothing else does either) then it should be marked for garbage collection regardless of the fact b still points to c.

If you’re curious what happens when objects refer to one one another (cyclic references and such) but all accessible references to those objects have been removed it gets a bit more complex. You can check this description on the mechanism if you’re curious-
https://devguide.python.org/garbage_collector/

thanks a lot , that clarifies it. Yes, it is not symmetric with the head_node since it is pointed to by something, probably, self.head_node that keeps the list “alive”.
thanks again :slight_smile:

1 Like

Can someone help me understand why one of these works and the other does not?

When I run this:
image

If I use if current_node.get_value() == value_to_remove: the code runs as expected.


Output
image

But if I use if current_node == value_to_remove: It comes back as false.

Why? The values are the same.


Output
image

Maybe it’s out of topic. What should I do, if I want to delete the values ​​in LinkedList. while in the LinkedList, there are several nodes that have the same value. How do I delete all nodes that match the value I want when calling the remove_node method. Here is my code.

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

# Our LinkedList class
class LinkedList:
  def __init__(self, value=None):
    self.head_node = Node(value)
  
  def get_head_node(self):
    return self.head_node
  
  def insert_beginning(self, new_value):
    new_node = Node(new_value)
    new_node.set_next_node(self.head_node)
    self.head_node = new_node
    
  def stringify_list(self):
    string_list = ""
    current_node = self.get_head_node()
    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):
    current_node = self.get_head_node()
    if current_node.get_value() == value_to_remove:
      self.head_node = current_node.get_next_node()
    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

Jika saya jalankan code dibawah ini

ll = LinkedList(5)
ll.insert_beginning(70)
ll.insert_beginning(5)
ll.insert_beginning(90)
ll.insert_beginning(70)
ll.insert_beginning(90)
print(ll.stringify_list())
ll.remove_node(90)
print(ll.stringify_list())

the output will be like this
Capture

From the results above, it can be concluded, we have to delete the values ​​from the nodes one by one. How do I if I want to delete all nodes with the value I gave to the remove_node method. For example, I want to delete all nodes with a value of 70. The result will be when we run remove_node (70) then the output of nodes with a value of 70 will be deleted.

My code cannot be verified and I have being trying for hours. I cant proceed without verifications. Is there any server problem? my connection is fine.

Welcome to the forums!

Please post your code (and format it according to this post) and describe the exact issue you are having (any error messages, parts you aren’t sure about, etc.).

If you are having connectivity issues, see this post.

I was checking the solution provided and the solution seems to break if we pass in a value which does not exist in the list…

Here is how I modified the code to ensure that even if the value is not in the list, it does not result in a run time error, I guess the other way would be to add a try, except block

def remove_node(self,value_to_remove):
current_node = self.get_head_node()
next_node = current_node.get_next_node()
if current_node.get_value() == value_to_remove:
self.head_node = self.head_node.get_next_node()
else:
while next_node:
if next_node.get_value() == value_to_remove:
current_node.set_next_node(next_node.get_next_node())
next_node = None
else:
current_node = current_node.get_next_node()
next_node = next_node.get_next_node()

if you don’t want to stop you can remove
current_node = None

so that you will not stop the while loop after finding the first value…

You will also need to remove the else block as you don’t want to stop if the value is found on the head node either

Whoever created this exercise seems to completely miss the notion of step-by-step instructions. I feel like if I have been just taught an alphabet and immediately asked to write an assay. What is missing is more prior exercises on the structure of linked lists and how to reference different nodes and traverse the list. I have to struggle for days and look for explanations on different resources. That makes me wonder if being on codecademy is even worth it?

Suddenly felt that I had to say it. We come here with such a different base of knowledge, somebody says that the course “feeds with a teaspoon”, somebody says that the explanation is too week… I haven’t try any other resourсes to study, so I have nothing to compare it with, but each time I feel the deepest respect for all the creators of codecademy for the patience and the ability to maintain a line without going too deep in details and not slipping too much on the surface, to teach people who want to study. It’s like in real life, you never find people who think exactly like you, lukily for us… But one can learn something from any person SORRY, SORRY, SORRY, it’s absoulutly our of topic!!! With great respect…

2 Likes

I still don’t understand why the instruction asked to set the current_node = None to exit the loop. Assuming we have a(head) -->b–>c, and after we find (current_next_node) b value == value_to_remove, we link a -->c, so current node is still a. If we set current value as None, does it messed up a value??? Could anyone please explain this to me?
And since I was not sure writing current_value = none, I wrote the return in order to exit the loop instead. It did checked final step of instruction as completed, but could anyone tell me if this way work?

Because the while loop condition is current_node, as long as current_node is truth the loop will continue to iterate. By setting current_node to None, we exit the loop since None is falsy. It doesn’t mess up the value, because in this case pass-by-object-reference is used (good explanation here).

Returning would also work, but if at any point in the future you wanted to add more code inside the method after the while loop, it might not function that way you would want.

2 Likes

Thanks you Victoria, for your insightful answer :slight_smile: I appreciate that!

2 Likes

You could also replace that line with the ‘break’ keyword.