From ceadbab59e44ceaafa40de04ac1dc32d27e999f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=A1clav=20P=C5=99ib=C3=ADk?= Date: Fri, 17 Oct 2025 14:20:46 +0200 Subject: [PATCH] impl fix: cll remove - contains with only item caused exception --- .../customlist/CustomLinkedListImpl.java | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) 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