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) {
|
||||
|
||||
if (currentNode == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
CustomLinkedListNode<T> nextNode = currentNode.getNext();
|
||||
|
||||
switch (position) {
|
||||
@@ -83,26 +87,29 @@ public class CustomLinkedListImpl<T> implements CustomLinkedList<T> {
|
||||
@Override
|
||||
public void remove(T item) {
|
||||
|
||||
CustomLinkedListNode<T> 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<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);
|
||||
return;
|
||||
}
|
||||
|
||||
beforeNode.setNext(nodeToRemove.getNext());
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user