Skip to content

Commit b6581ea

Browse files
committed
Fix for Issue TheAlgorithms#773. Deleting an element that doesn't exist causes NPE
1 parent 9b78621 commit b6581ea

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

DataStructures/Lists/DoublyLinkedList.java

+7-2
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)