Java – Deleting First Node in Doubly Linked List

data-structures, java, linked-list

The following code deletes the first node in a doubly linked list.

public Node deleteFirst()    {        Node temp = first;        if(            last = null;        else   = null;            first =;        return temp;    }

If the list only contains 1 element we set the reference of last to null. My question is how come we do not set the reference of first to null ? would it even make a difference ?

Best Solution

you are missing brackets

this statement will get executed in any case because it is out side of if/else

first =;

this is not python

else { = null;   first =;}