diff --git a/DataStructures/Lists/DoublyLinkedList.java b/DataStructures/Lists/DoublyLinkedList.java index fd250d956faf..677487e1d2b0 100644 --- a/DataStructures/Lists/DoublyLinkedList.java +++ b/DataStructures/Lists/DoublyLinkedList.java @@ -116,8 +116,13 @@ public Link deleteTail() { public void delete(int x) { Link current = head; - while (current.value != x) // Find the position to delete - current = current.next; + while (current.value != x) {// Find the position to delete + if (current != tail) { + current = current.next; + } else {// If we reach the tail and the element is still not found + throw new RuntimeException("The element to be deleted does not exist!"); + } + } if (current == head) deleteHead();
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: