Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Implement a function remove_all that takes a Link, and a value, and remove any l

ID: 3675708 • Letter: I

Question

Implement a function remove_all that takes a Link, and a value, and remove any list node containing that value. You can assume the list already has at least one node containing value and the first element is never removed. Notice that you are not returning anything, so you should mutate the list.

def remove_all(link , value):
"""Remove all the nodes containing value. Assume there exists some
nodes to be removed and the first element is never removed.

>>> l1 = Link(0, Link(2, Link(2, Link(3, Link(1, Link(2, Link(3)))))))
>>> print_link(l1)
<0 2 2 3 1 2 3>
>>> remove_all(l1, 2)
>>> print_link(l1)
<0 3 1 3>
>>> remove_all(l1, 3)
>>> print_link(l1)
<0 1>
"""
"*** YOUR CODE HERE ***"

Explanation / Answer

def remove_all(link,value):
current = link
previous = None
found = False
while not found:
if current.getData() == value:
found = True
else:
previous = current
current = current.getNext()

if previous == None:
self.head = current.getNext()
else:
previous.setNext(current.getNext()

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote