logo

Crowdly

What are the issues with the following implementation of delete item on an unsor...

✅ Перевірена відповідь на це питання доступна нижче. Наші рішення, перевірені спільнотою, допомагають краще зрозуміти матеріал.

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 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:

current = previous.link

else:

previous = current

current = current.link

Більше питань подібних до цього

Хочете миттєвий доступ до всіх перевірених відповідей на learning.monash.edu?

Отримайте необмежений доступ до відповідей на екзаменаційні питання - встановіть розширення Crowdly зараз!