remove_by_value implementation in singly_linked_list_exercise.py
satadrubasu opened this issue · comments
Proposed below :
if not self.head:
return
if self.head.val == value:
self.head = self.head.next
return
itr = self.head
prev_node = None
while itr:
if itr.val == value:
prev_node.next = itr.next
prev_node = itr
itr = itr.next
You can simplify all the logic with just one while loop:
def remove_by_value(self, value_to_remove: Any) -> None:
itr = self.head
while itr:
if value_to_remove == itr.next_value.data:
itr.next_value = itr.next_value.next_value
return
itr = itr.next_value
raise ValueError(f"{value_to_remove = } not found in the linked list")