✅ The verified answer to this question is available below. Our community-reviewed solutions help you understand the material better.
What are the issues with the following implementation of delete item on an unsorted linked list? You can assume self.head points to the node at the beginning of the list and each node has a link instance variable that refers to the next node in the list (or None if it's the last node).
def __delitem__(self, key) -> None:
if self.is_empty():
raise ValueError("Can't delete from an empty list")
previous = None
current = self.head
for _ in range(len(self)):
if current.item == key:
if current == self.head:
self.head = self.head.link
else:
current.link = previous.link
length -= 1
else:
previous = current
current = current.link
Get Unlimited Answers To Exam Questions - Install Crowdly Extension Now!