This community-built FAQ covers the “Removing by Value II” exercise from the lesson “Doubly Linked Lists: Python”.
Paths and Courses
This exercise can be found in the following Codecademy content:
Pass the Technical Interview with Python
FAQs on the exercise Removing by Value II
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!
You can also find further discussion and get answers to your questions over in #get-help.
Agree with a comment or answer? Like (
) to up-vote the contribution!
Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.
Looking for motivation to keep learning? Join our wider discussions in #community
Learn more about how to use this guide.
Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting
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!
I find this whole thing fascinating but it quickly becomes hard to follow, is this normal? Should I do everything from the beginning over again till I understand it? Does anyone have any other resources that may help me follow?
1 Like
I came up with a different solution and I was wondering if someone could tell me if it is correct.
My solution:
def remove_by_value(self, value_to_remove):
if value_to_remove == None:
return None
elif value_to_remove == self.head_node.get_value():
self.remove_head()
elif value_to_remove == self.tail_node.get_value():
self.remove_tail()
else:
current_node = self.head_node
while current_node:
next_node = current_node.get_next_node()
prev_node = current_node.get_prev_node()
if current_node.get_value() == value_to_remove:
prev_node.set_next_node(next_node)
next_node.set_prev_node(prev_node)
return current_node
current_node = next_node
Given solution:
def remove_by_value(self, value_to_remove):
node_to_remove = None
current_node = self.head_node
while current_node != None:
if current_node.get_value() == value_to_remove:
node_to_remove = current_node
break
current_node = current_node.get_next_node()
if node_to_remove == None:
return None
if node_to_remove == self.head_node:
self.remove_head()
elif node_to_remove == self.tail_node:
self.remove_tail()
else:
next_node = node_to_remove.get_next_node()
prev_node = node_to_remove.get_prev_node()
next_node.set_prev_node(prev_node)
prev_node.set_next_node(next_node)
return node_to_remove
2 Likes
I had a question about removing by value II in Double Linked Lists, for the node to be removed we dereference it by:
[python]
next_node = node_to_remove.get_next_node()
prev_node = node_to_remove.get_prev_node()
next_node.set_prev_node(prev_node)
prev_node.set_next_node(next_node)
[/python]
Here node_to_remove is the node containing the value to be removed. We change the links of it’s next node and previous node to link each other, but there is no mention of dereferencing node_to_remove’s next_node and prev_node. Like:
[python]
node_to_remove.set_next_node(None);
node_to_remove.set_prev_node(None);
[/python]
I can understand why it’s unnecessary coz when we traverse the double-linked list, it will skip over node_to_remove. But, I was wondering, would it still exist in the computer memory?? If we had to remove multiple values in b/w wouldn’t it be a problem??
Works and I like your implementation way better!
def remove_by_value(self, value_to_remove):
node_to_remove = None
current_node = self.head_node
while current_node != None:
if current_node.get_value() == value_to_remove:
node_to_remove = current_node
break
current_node = current_node.get_next_node()
if node_to_remove == None:
return None
if node_to_remove == self.head_node:
self.remove_head()
#added code for step 2 below
elif node_to_remove == self.tail_node:
self.remove_tail()
i hit run on step 2 and the button turns into a spinning wheel. waiting, waiting, waiting, then:
Failed to test your code.
i delete the code and it still fails, delete more same result, what is wrong?
wow, i didn’t see the fact that the while was infinite, human error