Learn Java
import com.learnjava.datastructurescollections.Linkedlist;

LinkedList in Java

@Concept

LinkedList, also from java.util, stores elements as a chain of individually-linked nodes rather than one contiguous array — each node holds a value plus a reference to the next (and previous, since Java's is doubly-linked) node in the chain. That structural difference flips ArrayList's performance trade-offs: inserting or removing at the front or middle is fast, since it only means relinking a couple of neighboring nodes, but reaching an arbitrary index requires walking the chain node by node from one end.

LinkedList also implements the Deque interface, giving it efficient addFirst, addLast, removeFirst, and removeLast operations — which is why it's a common choice specifically when a program needs a queue or a stack rather than general-purpose indexed access.

@HowItWorks
1

Node-based structure

Each element lives in its own node, holding a value and links to its neighboring nodes, rather than sitting in one shared array.

2

Fast insertion and removal at known positions

Adding or removing right after a node the program already has a reference to only means relinking a couple of pointers.

3

Slow indexed access

get(index) has to walk the chain from the nearest end, one node at a time, until it reaches that position.

4

Doubly-linked in Java

Each node links both forward and backward, which is why LinkedList can walk efficiently in either direction.

5

Deque operations

addFirst, addLast, removeFirst, and removeLast are all fast, making LinkedList a natural fit for stack or queue behavior.

@Example
import java.util.LinkedList;
 
public class LinkedListDemo {
public static void main(String[] args) {
LinkedList<String> queue = new LinkedList<>();
queue.addLast("first");
queue.addLast("second");
queue.addFirst("zeroth");
System.out.println(queue);
System.out.println("Removed: " + queue.removeFirst());
}
}
Output
[zeroth, first, second]
Removed: zeroth
1addLast appends "first" then "second" to the end of the chain, one node at a time.
2addFirst attaches "zeroth" as a brand-new first node, relinking only the chain's front, with no shifting of any other node involved.
3Printing the list shows all three elements in their current chain order.
4removeFirst detaches and returns the front node's value, "zeroth", relinking the chain to start at "first" instead.
@Complexity
time addFirst/addLast/removeFirst/removeLast: O(1); get(index): O(n)
space O(n)

Every element carries the extra overhead of its node links, and reaching an arbitrary index means walking the chain — there's no shortcut the way direct array indexing provides.

@UseCases
Implementing a queue or stack where elements are added and removed from the ends frequently
Building a data structure where frequent insertion in the middle matters more than random access by index
Choosing LinkedList over ArrayList specifically for its Deque operations
Recognizing when LinkedList's slow get(index) makes ArrayList the better choice instead

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.