Merge pull request #774 from abhijay94/master
Fix #773 (Deleting an element that doesn't exist causes NPE)
This commit is contained in:
commit
990b88f72f
@ -116,8 +116,13 @@ public class DoublyLinkedList {
|
|||||||
public void delete(int x) {
|
public void delete(int x) {
|
||||||
Link current = head;
|
Link current = head;
|
||||||
|
|
||||||
while (current.value != x) // Find the position to delete
|
while (current.value != x) {// Find the position to delete
|
||||||
current = current.next;
|
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)
|
if (current == head)
|
||||||
deleteHead();
|
deleteHead();
|
||||||
|
Loading…
Reference in New Issue
Block a user