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(first.next==null)            last = null;        else            first.next.previous = null;            first = first.next;        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 = first.next;

this is not python

else {   first.next.previous = null;   first = first.next;}