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 () 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 () below!
Agree with a comment or answer? Like () to up-vote the contribution!
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
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.
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?
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!
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
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
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
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.
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.