01 / 21

ArrayDeque vs.
LinkedList

Choosing the right Deque implementation in Java: when a resizable array beats a linked chain and vice versa.

Java

What Is a Deque?

A Deque (double-ended queue) is a linear collection that supports insertion and removal at both ends. In Java, the Deque<E> interface lives in java.util and extends Queue<E>.

A B C D E add remove add remove FRONT BACK

Inside ArrayDeque

ArrayDeque stores elements in a resizable circular array. Two integer indices, head and tail, track the front and back, wrapping around when they reach the end of the array.

0 1 2 3 4 5 6 7 -- 10 20 30 40 -- -- -- head tail wraps around

Inside LinkedList

LinkedList is a doubly-linked list. Each element lives in its own Node object that holds pointers to the previous and next nodes.

10 Node 20 Node 30 Node 40 Node null null first last Each node: prev + next pointers + data = more memory per element

Why the Choice Matters

Both classes implement the same interface, so they are interchangeable in code. But internally they differ in memory layout, constant factors, and special capabilities. Picking the wrong one can cost you 2-4x performance in tight loops.

Speed
ArrayDeque's contiguous array is cache-friendly; LinkedList chases pointers across the heap.
Memory
ArrayDeque wastes some empty slots. LinkedList wastes 2 extra pointers per node (~16 bytes overhead each).
Features
LinkedList implements List<E> too (indexed access). ArrayDeque does not: it is Deque-only.

Time Complexity: Head-to-Head

Operation ArrayDeque LinkedList
addFirst / addLast Amortized \( O(1) \) \( O(1) \)
removeFirst / removeLast \( O(1) \) \( O(1) \)
peekFirst / peekLast \( O(1) \) \( O(1) \)
get(index) Not supported \( O(n) \)
Iteration (full scan) \( O(n) \) cache-friendly \( O(n) \) pointer-chasing
Memory per element ~1 reference ~3 references

The Key Distinction

ArrayDeque
Implements Deque only. Cannot hold null elements. Backed by a circular array: excellent cache locality. Best default for stacks and queues.
LinkedList
Implements Deque + List. Can hold null elements. Allocates a node per element. Choose when you need List API or null support.

null Elements

ArrayDeque uses null internally as a sentinel to detect empty slots. If you try to add null, it throws a NullPointerException. LinkedList has no such restriction.

Deque<String> ad = new ArrayDeque<>(); ad.add(null); // throws NullPointerException Deque<String> ll = new LinkedList<>(); ll.add(null); // works fine

How to Choose

1
Do you need indexed access? If yes, you need the List interface: choose LinkedList (but note \( O(n) \) get).
2
Must you store null elements? If yes: LinkedList. ArrayDeque forbids null.
3
Do you need a stack or queue only? If yes: ArrayDeque. It is faster and uses less memory.
4
Will you iterate frequently? ArrayDeque's contiguous memory gives a cache advantage.
5
Need constant-time removal of arbitrary elements via an iterator? Only LinkedList provides this in \( O(1) \). ArrayDeque's iterator removal can take up to \( O(n) \) time as it may require shifting elements.

Example 1: Undo Stack

You are building a text editor with an undo feature. Every action is pushed onto a stack, and undo pops the most recent one. No nulls, no indexed access needed.

// Best choice: ArrayDeque as a stack Deque<Action> undoStack = new ArrayDeque<>(); undoStack.push(action); // addFirst — O(1) Action last = undoStack.pop(); // removeFirst — O(1)

Example 2: BFS Queue

Breadth-first search processes nodes in FIFO order. You enqueue at the back, dequeue from the front. Nodes are never null.

// Best choice: ArrayDeque as a queue Deque<TreeNode> queue = new ArrayDeque<>(); queue.offer(root); // addLast — O(1) TreeNode curr = queue.poll(); // removeFirst — O(1)
A B C D poll offer ArrayDeque: fast FIFO

Example 3: When LinkedList Wins

A music player maintains a playlist that users can navigate forward/backward (deque) and also jump to by index (list). Null entries represent "ad breaks."

// Need both Deque AND List behavior, plus null LinkedList<Song> playlist = new LinkedList<>(); playlist.addLast(song); // Deque: enqueue playlist.addLast(null); // null = ad break Song s = playlist.get(3); // List: random access O(n) playlist.removeFirst(); // Deque: advance

Cache Locality Explained

ArrayDeque (contiguous) 10 20 30 40 50 60 1 cache line loads many elements LinkedList (scattered) 10 20 30 40 cache miss cache miss cache miss Each node may be far apart in memory

ArrayDeque Resizing

When the internal array is full, ArrayDeque doubles its capacity and copies all elements to a new array. This single expensive \( O(n) \) copy is spread across all the previous insertions, yielding amortized \( O(1) \).

Capacity 4 (full) A B C D double! Capacity 8 A B C D -- -- -- -- 4 empty slots ready for future adds

Common Mistakes

✕ Using Stack class
java.util.Stack extends Vector (synchronized, slow). Use ArrayDeque instead for stack behavior.
✕ LinkedList for queues
Defaulting to LinkedList for a simple FIFO queue wastes memory and hurts cache performance. ArrayDeque is faster.
✕ Nulls in ArrayDeque
Forgetting that ArrayDeque prohibits null and getting a NullPointerException at runtime.

Summary Comparison

Feature ArrayDeque LinkedList
Backing structure Resizable circular array Doubly-linked nodes
Implements List? No Yes
Allows null? No Yes
Memory efficiency Better Worse (node overhead)
Cache performance Excellent Poor
Best for stacks/queues ✓ Recommended Acceptable
Best for List + Deque Cannot ✓ Only option

Question 1 of 5

You need a LIFO stack for parsing matched parentheses. No nulls are stored. Which implementation should you choose?

Question 2 of 5

Your collection must support both indexed access (get by position) and deque operations (addFirst, removeLast). Which is the better choice?

Question 3 of 5

Why does ArrayDeque generally outperform LinkedList for queue operations, even though both have \( O(1) \) add/remove?

Question 4 of 5

What happens when you call arrayDeque.add(null)?

Question 5 of 5

You are implementing a BFS traversal of a graph with 100,000 nodes. No null values are needed. Which implementation minimizes memory usage?