Reputation: 831
I need to remove() from a (custom) LinkedList, setting last iterated item to null. There are two cases, one where prev is head, one where it is not. The nodes are next, prev and head. I was wondering if you spot any problems in the following code? Do I need to set the iterator variables in between? (or will emptying prev do the trick?) Thanks guys!
public void remove() {
if(prev == null) {
throw new IllegalStateException();
}
else {
if(prev == head){
head = head.next;
prev = null;
sizeOfList--;
}
else {
prev = null;
sizeOfList--;
}
}
}
Upvotes: 0
Views: 925
Reputation: 71009
In case your previous is not the head(second case in your code) you should fix the next link of the previous node before prev
. Otherwise it will be left pointing to prev
.
Upvotes: 1