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/

1 Like

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?

1 Like

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…

1 Like

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.

3 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.

I had to go to Youtube for help, Data Structures in Python: Singly Linked Lists -- Node Swap - YouTube

100% agreed. I had to go to Youtube for help and it’s wasting a lot of my time to learn what Codecademy didn’t properly teach in respectable length. Just felt rushed and then pushed a huge block of steps into one paragraph and told us to parse it out with vague instructions.

Why doesn’t Codecademy make these linked list lessons into video? It’s really hard to parse out current_node.set_next_node(next_node.get_next_node()) as a total beginner…like wow. What an awful way to present this already confusing content.

With all due respect, this has been one of the worst lessons I’ve come across on Codecademy. Perhaps in the future I’ll think of this lesson as beginner level, but RIGHT NOW, this is hard!

I was fine with the previous chapter of Nodes. I was able to draw the nodes out on a paper and follow through each step of the code, but once I got into Linked List lesson, I couldn’t follow.

I often got lost on what node I am on because all I see is repetitive words “node, next.node, new_node, Node…” and a vague set of instructions of what I’m supposed to do. I kept losing focus on what my purpose is…even when I retrace my steps.

I and a huge part of Codecademy community learn better when we actually visualize the logical steps taken for each step of the code.

I’m sure the lesson afterwards is going to be much more complicated but if this is level of explanation we are getting at this stage, then we are pretty much set up for failure. Why even bother with Codecademy in the first place?

My advice is for Codecademy to invest on a couple of visual and motion designers (happy to volunteer) to communicate this technical jargon. It can be an interactive lesson or even a simple video or gif (no image).

P.S I participated in one of your user research interviews about a year ago, I’m happy to assist again. :+1:

2 Likes

Hi,

Half way through doing this lesson, I realised that using the get_next_node() and get_head_node() functions could be completely bypassed by simply referencing the variables themselves.

Is there any technical issues with doing this in the long term or is it just there for good practise?

There was a forum post the other day that I think matched your query, I think my reply might cover your issue-

Hey, I too thought about that too much and then got that it’s simply to stop the while loop. As it is the end of the program and after it, we are not using current_node so doing this step current_node = None, will end the while loop without affecting the program.