Codecademy Forums

FAQ: Linked Lists: Python - Linked List Implementation III

This community-built FAQ covers the “Linked List Implementation III” exercise from the lesson “Linked Lists: Python”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science

Linear Data Structures

FAQs on the exercise Linked List Implementation III

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hello!

So I reported the bug, but the instructions for this exercise show as completed and won’t reset and no errors arise no matter what I do (I tried changing all indentation to test that error messages won’t arise), so I don’t know if my code is correct or not and since it is my first data structure, I don’t know how to check it without the instructions checking each step off. I am hoping you all can help me. I am copying my method below. I am fairly confident that up until the “else:” is correct, but I am very unsure about the rest. Thank you for any feedback!

Define your remove_node method below:

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:
      current_next_node = current_node.get_next_node()
      if current_next_node == value_to_remove:
        current_node.set_next_node = next_node.get_next_node()
        current_node = None

I’m trying to understand the part where you

current_node = None

So assuming we have Head --> b --> c & b is what is being removed

So the current_node at this point is still the head node Node( #, b) then we set the current node to skip b to go to c. So now head node (current_node) is Node(#, c). Why set current_node = None when it is in fact Node(#,c).

I just answered my own question… you set current_node to None because current_node has served it’s purpose and is not needed anymore and it will send a signal to the while loop that you may stop.

1 Like

I was told the reset issue is happening to only some people. I’m trying to check my code as well and the checks won’t reset.

If someone could go back and reset and post the solutions version, that would be incredibly helpful! Please!

Here is the solution:


  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

I had come up with something similar but functionally the same but the checker kept asking me if I accidentally removed my remove_node() method. :man_facepalming:

1 Like

These getters and setters kind of come from nowhere, with no explanation. Why are they even necessary, when the node values can be referenced directly from within their own class? I wrote the remove_node method with out any getters and setters and it works exactly the same… Looking for understanding on WHY? What is the advantage or reasoning behind the practice?

def remove_node(self, value_to_remove):
    print('Remove: ' + str(value_to_remove))
    current_node = self.head_node
    if current_node.value == value_to_remove:
      print(str(current_node.value))
      self.head_node = current_node.next_node
    else:
      while current_node:
        nxt_node = current_node.next_node
        if nxt_node.value == value_to_remove:
          current_node.next_node = nxt_node.next_node
          current_node = None
        else:
          current_node = nxt_node

Found this discussion fairly helpful:

https://www.python-course.eu/python3_properties.php

1 Like

My guess is people see that getters and setters exist and therefore assume that they are automagically useful.

Use what you can argue for.

Something that is particularly useless is having get and set behaving the same way as a plain attribute.

Did the instructions or hints mention the second else statement?

The instructors apparently learned their OOP via Java, where getters & setters are mandatory. But they’re still inconsistent, as there is no setter for head_node. That one we apparently are to access directly!

1 Like

From what I seen whilst I was doing that exercise, the second else statement wasn’t mentioned in the hints and definitely not in the instructions.

This exercise has absolutely killed me, but I think I finally understand it now :slight_smile:

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?

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

Hey,

So I’m learning as well but here’s how I see it:

1 - ‘current_node’ is a separate variable which is assigned a node’s value but is not the node itself (i.e. the node it refers to remains unchanged).

2 - ‘while current_node:’ is constructed to continue running while ‘current_node’ has a value.

3 - Once the function has found the node to remove, removed it, and re-linked the orphaned node, current_node still has a value and consequently the ‘while’ loop will continue to run.

4 - By setting current_node = None it clears the ‘while’ condition and breaks the loop. Your ‘return’ does the same thing.

if you don’t want clients to be able to mess with the value, you make them private and only accessible through a get() method
But if you have a set method, it’s kinda pointless unless it’s some specialized data structure where you want some “side effects” to occur when you set/get

was bored. instead made a remove_node that got rid of ALL nodes containing the value :grin:

Also, in the end
else:
current_node = next_node

Please explain this.

We are marching down a chain of nodes, a->b->c->d->e , implementing the following strategy:

At each node, we name our position current_node, look ahead at the following (next) node, and decide if that is the one to remove.

If it is, we remove that node by re-linking current_node to the node which follows the doomed next_node. That leaves next_node “orphaned”, or no longer a part of the chain.

If it is not, (to answer your question) we move one step down the chain with current_node = next_node

Thanks! Helped me understand.

1 Like

Hey, I am struggling with the same question. Let me know if you find the answer please
I have the same feeling that if you set current_node as None, it will break the chain.

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:
      pass

This is what I have so far and I can’t get any further because I’m getting the message “Did you remove your remove_node() method accidentally?” and I have no idea what is going on.

(the else: pass is just so I don’t get errors when the code actually works)
If I delete everything and start again it works till I get to the if statement.