TheAlgorithms/Java · error · RuntimeException
The element to be deleted does not exist!
Error message
The element to be deleted does not exist!
What it means
Thrown by DoublyLinkedList.delete(int x) when the value x is not found in any node. The method walks from head to tail comparing values; reaching tail without a match triggers RuntimeException. Unlike an IndexOutOfBounds, this is a value-not-found condition.
Source
Thrown at src/main/java/com/thealgorithms/datastructures/lists/DoublyLinkedList.java:305
}
--size;
return temp;
}
/**
* Delete the element from somewhere in the list
*
* @param x element to be deleted
* @return Link deleted
*/
public void delete(int x) {
Link current = head;
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();
} else if (current == tail) {
deleteTail();
} else { // Before: 1 <--> 2(current) <--> 3
current.previous.next = current.next; // 1 --> 3
current.next.previous = current.previous; // 1 <--> 3
}
--size;
}
/**
* Inserts element and reorders
*
* @param x Element to be addedView on GitHub (pinned to fdfb9a395b)
Solutions
- Check membership with a contains/search before delete.
- Catch RuntimeException (or a custom subclass) and treat not-found as a no-op.
- Make delete idempotent by wrapping it: try { delete(x); } catch (RuntimeException ignored) {}.
- Avoid double-processing the same value across threads with a coordinating lock or set.
Example fix
// before
dll.delete(x); // throws if x absent
// after
try {
dll.delete(x);
} catch (RuntimeException e) {
// element already absent — safe to ignore
} Defensive patterns
Strategy: try-catch
Try / catch
try {
dll.delete(x);
} catch (RuntimeException e) {
// element not present — treat as no-op
} Prevention
- Make delete idempotent by catching RuntimeException when not-found is acceptable.
- Check membership before delete if you need to distinguish absent vs present.
- Coordinate concurrent deletes to avoid double-processing.
When it happens
Trigger: Deleting a value never inserted. Deleting after the element was already removed. Type mismatch where int comparison silently fails (e.g., expecting a value from a different domain).
Common situations: Idempotent cleanup code that retries deletes. Concurrent deletes where one thread removes the value before another. Integer value confusion (signed/unsigned, encoding).
Related errors
- Cannot add null element to the list
- Position out of bounds
- Cannot add null element to the list
- Position out of bounds
- Element cannot be null
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/6466d6d137f08f43.
Report an issue: GitHub.