Skip to content

Commit d64a3e9

Browse files
authored
Update singly_linked_list_exercise.py
1 parent 6b8a495 commit d64a3e9

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

data_structures/3_LinkedList/Solution/singly_linked_list_exercise.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,20 @@ def remove_by_value(self, data):
110110
if self.head.data == data:
111111
self.head = self.head.next
112112
return
113-
113+
114+
initial_length = self.get_length()
115+
count = 0
114116
itr = self.head
115117
while itr.next:
116118
if itr.next.data == data:
117119
itr.next = itr.next.next
118120
break
119121
itr = itr.next
122+
count += 1
123+
124+
if count == initial_length:
125+
raise Exception("The specified element is not present in the linked list")
126+
120127

121128
if __name__ == '__main__':
122129
ll = LinkedList()

0 commit comments

Comments
 (0)