LinkedList is a linear data structure that implements both the List and Deque interfaces. Instead of contiguous array memory, elements in a LinkedList are stored in separate, non-contiguous Node objects linked together via bidirectional memory pointers.
Every element is wrapped inside a private static inner class Node<E> on the Heap:
// Internal JDK Node structure:
private static class Node<E> {
E item; // Actual data payload
Node<E> next; // Pointer address to next node
Node<E> prev; // Pointer address to previous node
}
| Feature | ArrayList |
LinkedList |
|---|---|---|
| Underlying Structure | Contiguous resizable array | Doubly-linked nodes across Heap |
Random Access (get(i)) |
O(1) Instant (Direct offset) | O(n) Slow (Must traverse from head/tail) |
Insert at Head (add(0, x)) |
O(n) Slow (Shifts entire array) | O(1) Instant (Updates 2 node pointers) |
| Memory Consumption | Low (Stores raw references) | High (Each node allocates 2 pointer addresses) |
| CPU Cache Friendliness | Excellent (Contiguous cache lines) | Poor (Scattered Heap memory hops) |
import java.util.LinkedList;
public class LinkedListDequeDemo {
public static void main(String[] args) {
LinkedList<String> queue = new LinkedList<>();
// Adding elements to ends
queue.addFirst("Header Item");
queue.addLast("Middle Task");
queue.addLast("Footer Task");
System.out.println("First Item: " + queue.getFirst()); // Header Item
System.out.println("Last Item: " + queue.getLast()); // Footer Task
// Queue Poll (Removes from head)
String completed = queue.removeFirst();
System.out.println("Processed: " + completed);
System.out.println("Remaining queue: " + queue);
}
}
Now let us examine Java's legacy synchronized list classes: Vector and Stack. Proceed to: Java Vector & Stack →
Accelerate your Java mastery by studying these tightly connected core tutorials:
Dynamic array comparison.
FIFO and LIFO queues.
Legacy synchronized lists.
Traversing linked nodes.