impl fix: cll remove - contains with only item caused exception
This commit is contained in:
@@ -33,6 +33,10 @@ public class CustomLinkedListImpl<T> implements CustomLinkedList<T> {
|
|||||||
|
|
||||||
private CustomLinkedListNode<T> traverseFind(CustomLinkedListNode<T> currentNode, T item, ItemPosition position) {
|
private CustomLinkedListNode<T> traverseFind(CustomLinkedListNode<T> currentNode, T item, ItemPosition position) {
|
||||||
|
|
||||||
|
if (currentNode == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
CustomLinkedListNode<T> nextNode = currentNode.getNext();
|
CustomLinkedListNode<T> nextNode = currentNode.getNext();
|
||||||
|
|
||||||
switch (position) {
|
switch (position) {
|
||||||
@@ -83,26 +87,29 @@ public class CustomLinkedListImpl<T> implements CustomLinkedList<T> {
|
|||||||
@Override
|
@Override
|
||||||
public void remove(T item) {
|
public void remove(T item) {
|
||||||
|
|
||||||
CustomLinkedListNode<T> nodeToRemove = traverseFind(head, item, ItemPosition.CURRENT);
|
if (head == null) {
|
||||||
|
|
||||||
if (nodeToRemove == null) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (nodeToRemove == head) {
|
if (head.getItem().equals(item)) {
|
||||||
head = head.getNext();
|
head = head.getNext();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
CustomLinkedListNode<T> beforeNode = traverseFind(head, item, ItemPosition.BEFORE);
|
CustomLinkedListNode<T> beforeNode = traverseFind(head, item, ItemPosition.BEFORE);
|
||||||
|
|
||||||
if (nodeToRemove.getNext() == null) {
|
if (beforeNode == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
CustomLinkedListNode<T> nodeToRemove = beforeNode.getNext();
|
||||||
|
|
||||||
|
if (beforeNode.getNext() == null) {
|
||||||
beforeNode.setNext(null);
|
beforeNode.setNext(null);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
beforeNode.setNext(nodeToRemove.getNext());
|
beforeNode.setNext(nodeToRemove.getNext());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Reference in New Issue
Block a user