Reputation: 11
In LinkedList we normally assign null value to last node and also use this condition to check for the last node.
I am checking for the last node with the same condition either its "next" node link is null or not. But I'm unable to handle NullPointerException when I get null value by the method "getNext".
while(lastNode.getNext() != null)
{
lastNode= lastNode.getNext();
}
Upvotes: 1
Views: 2825
Reputation: 69
I assume this is a custom implementation of a LinkedList; java.util.LinkedList
does not have a getNext()
method.
That said, what you want is:
while (current != null) {
past = current;
current = current.getNext();
}
return past;
I am assuming here that you want to return the last node, and that past
is a variable of the same type as current
.
Upvotes: 1