How LinkedList works internally in Java?

How LinkedList works internally in java is one of the most favorite questions of interviewers. Also, it is good to know about a data structure a programmer is using so that we can take some precautions for performance.

Internally, LinkedList is implemented as a doubly-linked list. See the Node structure below:

private static class Node<E>{

E item;

Node<E> next;

Node<E> prev;

Node(Node<E> param1, E paramE, Node<E> param2)


this.previous = param1; = param2

this.item = paramE;



Obviously, previous contains the previous Node and next contains the next Node. If not present, it contains null.

To traverse the LinkedList in forward way, we need the first element, the header and to traverse the LinkedList in backward way, we need the last element.

Both these references are class level variables:

transient Node<E> first;

transient Node<E> last;

Here is how the add() function of LinkedList works:

public boolean add(E paramE)



return true;


add functions adds the element to the last of the list. This way, it maintains the insertion order.

Now, lets see how this linkLast() works:

void linkLast(E paramE)
Node localNode1 = this.last;
Node localNode2 = new Node(localNode1, paramE, null);
this.last = localNode2;
if(localNode1 == null)
this.first = localNode2;
else = localNode2;

this.size +=1;
  1. It takes the last element (localNode1).
  2. It creates the new Node (localNode2).
  3. It adds null to the next. Because this will be the last Node of the list and there will be  nothing to point to next.
  4. It adds previously last Node(localNode1) as previous Node. Here, it also checks if the current element is the first element in the list by checking the null on previous last Node. null will mean that there was no any previous Node. In that case,  it sets the header as current Node.

Let me know your thoughts and questions in comment section.

Ankush Pahwa is an IT Majdoor (Labourer) in Indian IT industry for last 5 years. Don’t know what else to say, so stopping here.

Self-Learning -> Focused Group -> Learning Intensification -> Self-Learning

Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *

LinkedIn Auto Publish Powered By :