diff --git a/assignment/src/main/java/appointmentplanner/customlist/CustomLinkedListImpl.java b/assignment/src/main/java/appointmentplanner/customlist/CustomLinkedListImpl.java index 5e49219..ea46089 100644 --- a/assignment/src/main/java/appointmentplanner/customlist/CustomLinkedListImpl.java +++ b/assignment/src/main/java/appointmentplanner/customlist/CustomLinkedListImpl.java @@ -33,6 +33,10 @@ public class CustomLinkedListImpl implements CustomLinkedList { private CustomLinkedListNode traverseFind(CustomLinkedListNode currentNode, T item, ItemPosition position) { + if (currentNode == null) { + return null; + } + CustomLinkedListNode nextNode = currentNode.getNext(); switch (position) { @@ -83,26 +87,29 @@ public class CustomLinkedListImpl implements CustomLinkedList { @Override public void remove(T item) { - CustomLinkedListNode nodeToRemove = traverseFind(head, item, ItemPosition.CURRENT); - - if (nodeToRemove == null) { + if (head == null) { return; } - if (nodeToRemove == head) { + if (head.getItem().equals(item)) { head = head.getNext(); return; } CustomLinkedListNode beforeNode = traverseFind(head, item, ItemPosition.BEFORE); - if (nodeToRemove.getNext() == null) { + if (beforeNode == null) { + return; + } + + CustomLinkedListNode nodeToRemove = beforeNode.getNext(); + + if (beforeNode.getNext() == null) { beforeNode.setNext(null); return; } beforeNode.setNext(nodeToRemove.getNext()); - } @Override