From d64a3e9108bb894b25680dac5fc17f0c9d9e4b13 Mon Sep 17 00:00:00 2001 From: Inamul2 <70210601+Inamul2@users.noreply.github.com> Date: Wed, 6 Apr 2022 03:16:53 +0530 Subject: [PATCH] Update singly_linked_list_exercise.py --- .../3_LinkedList/Solution/singly_linked_list_exercise.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/data_structures/3_LinkedList/Solution/singly_linked_list_exercise.py b/data_structures/3_LinkedList/Solution/singly_linked_list_exercise.py index cce871c..d5aa057 100644 --- a/data_structures/3_LinkedList/Solution/singly_linked_list_exercise.py +++ b/data_structures/3_LinkedList/Solution/singly_linked_list_exercise.py @@ -110,13 +110,20 @@ def remove_by_value(self, data): if self.head.data == data: self.head = self.head.next return - + + initial_length = self.get_length() + count = 0 itr = self.head while itr.next: if itr.next.data == data: itr.next = itr.next.next break itr = itr.next + count += 1 + + if count == initial_length: + raise Exception("The specified element is not present in the linked list") + if __name__ == '__main__': ll = LinkedList()
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: