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!
What if there was a add_by_value() method that adds a node to the linked list even if the value is not the head node or tail node like the reverse of the remove_by_value() method? ? How could I make that and what can a solution code of that be?
Try the following. It seems to work… I added a “previous value” parameter in order to specify where exactly to add the new value. You have to provide something since they’re all connected to each other and you want to keep / update the links.
So, having done all this, I have to wonder, why do we have to create the get/set functions?
why not just directly edit the elements?
I mean typing:
next_node = current_node.get_next_node()
prev_node = current_node.get_prev_node()
new_node.set_next_node(next_node)
new_node.set_prev_node(prev_node)
vs
new_node.next_node = current_node.next_node
new_node.prev_node = current_node.prev_node
I mean, I suppose if you wanted to do more in your get/set functions other than just assignment, I would understand.
For some general discussion about this style of programming (accessors or getters/setters) I think Wikipedia has a nice generic introduction but you can get more information suited to your level if you search around-
In Python you might find more use of properties than straightforward setters as they’re directly supported as a built-in in the language. There’s some discussion on what a property is here:
When it comes to the lessons the tests are strict and expect a certain style so that’s what you’ll need to stick with. Hopefully the given links helps with the why and the potentially more convenient alternatives available in Python.
while current_left is not None and current_right is not None:
if current_left.get_value() == value_to_remove:
node_to_remove = current_node
break
if current_right.get_value() == value_to_remove:
node_to_remove = current_node
break
if current_left.get_prev_node() == current_right:
return None
current_right = current_right.get_prev_node()
current_left = current_left.get_prev_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
Does this run at O(log n) time complexity ? Since standard process has O(n) time complexity as you might have to traverse the whole linked list, nth time to find the value. But, this process you have two pointers looking for the value from left and right which I assume will reduce the time by half.