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?