Skip to content

Commit 990b88f

Browse files
authored
Merge pull request TheAlgorithms#774 from abhijay94/master
Fix TheAlgorithms#773 (Deleting an element that doesn't exist causes NPE)
2 parents 0d1110a + b6581ea commit 990b88f

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

DataStructures/Lists/DoublyLinkedList.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,13 @@ public Link deleteTail() {
116116
public void delete(int x) {
117117
Link current = head;
118118

119-
while (current.value != x) // Find the position to delete
120-
current = current.next;
119+
while (current.value != x) {// Find the position to delete
120+
if (current != tail) {
121+
current = current.next;
122+
} else {// If we reach the tail and the element is still not found
123+
throw new RuntimeException("The element to be deleted does not exist!");
124+
}
125+
}
121126

122127
if (current == head)
123128
deleteHead();

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

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:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy