Scrivio Interview library
InternalsIntermediate7 min read · updated 12 November 2026

Why does ArrayDeque usually outperform LinkedList as a stack or queue in Java? Explain the memory layout and when LinkedList is still justified.

ArrayDeque keeps its elements in one circular array with head and tail indexes, so pushing and popping at either end is amortized O(1), allocates nothing per element and reads memory that sits close together. LinkedList allocates a separate node of about 24 bytes (more without compressed pointers) for every element, which costs memory, garbage collection work and cache misses. LinkedList is still justified only when we need null elements or O(1) removal at a position we already hold through a ListIterator, and the legacy Stack class should be replaced by ArrayDeque behind the Deque interface.

Why this question is asked

Both classes implement Deque, so they look interchangeable, and many codebases still use LinkedList as a queue out of habit. Interviewers ask this question because the answer is not about big-O at all: both have O(1) operations at the ends. The difference lies in memory layout and how hardware and the garbage collector deal with it. In this answer, we will recap the two layouts, compare their memory, see why locality and allocation matter, check the behavioural differences such as nulls, look at the rare case where LinkedList wins, and replace the old Stack class.

1Two Layouts for the Same Interface

A deque, short for double-ended queue, lets us add and remove at both ends. Used from one end it is a stack (last in, first out); used from both ends it is a queue (first in, first out).

ArrayDeque stores elements in an Object[] and keeps two indexes: head, the position of the first element, and tail, the position after the last. Adding at the end writes at tail and moves tail forward; adding at the front moves head backward and writes there. When an index runs off either end of the array, it wraps around to the other side, so the array is used as a ring. When the array fills up, ArrayDeque allocates a bigger one and copies the elements in order; since Java 9 it roughly doubles small arrays and grows larger ones by half, so adds are amortized O(1).

LinkedList is a doubly linked list. Every element lives in its own Node object holding the element, a pointer to the previous node and a pointer to the next one. The list keeps pointers to the first and last nodes. Adding at either end allocates a node and fixes a few pointers, which is O(1) every time.

Memory for 1 million Integer elements (compressed pointers)
StructurePer elementTotalObjects
ArrayDeque4-byte slot, some spare capacityabout 4 to 8 MB1 array
LinkedList24-byte nodeabout 24 MB1 million nodes
Both also hold the 16-byte Integer objects themselves; without compressed pointers a node takes 40 bytes.

2Why Locality and Allocation Decide the Winner

The difference in memory is only part of the story. ArrayDeque's elements are references stored next to each other in one array, so iterating or popping in sequence reads memory in order. The CPU loads memory in 64-byte cache lines and prefetches the next ones, so 16 consecutive references arrive with one or two memory accesses.

LinkedList's nodes are allocated one at a time and, after some garbage collections and other allocations in between, end up spread around the heap. Moving to the next element means reading the next pointer and then fetching a node that may be anywhere, a cache miss that costs around 100 nanoseconds. Every add also allocates a node, and every remove leaves one for the garbage collector. A queue that processes a million messages per second therefore creates a million short-lived objects per second with LinkedList and none with ArrayDeque, once its array has grown to its working size.

Java
Deque<Integer> stack = new ArrayDeque<>();
stack.push(1);
stack.push(2);
stack.push(3);
stack.pop(); // 3, last in first out
stack.peek(); // 2
Deque<String> queue = new ArrayDeque<>();
queue.offer("a");
queue.offer("b");
queue.poll(); // "a", first in first out

In simple JMH benchmarks, ArrayDeque is typically several times faster than LinkedList as a queue, and the gap grows when the collection is large or the heap is busy. As always, measure on your own workload.

3Behavioural Differences: Nulls and Indexes

ArrayDeque does not accept null. offer(null), push(null) and add(null) all throw NullPointerException. The reason is that methods such as poll() and peek() return null to mean "the deque is empty", so a stored null would be indistinguishable from emptiness. LinkedList allows null elements, which is one of the few functional reasons to keep it, although storing null in a queue is usually a design smell.

LinkedList also implements List, so it has get(i), set(i, x) and add(i, x). These are O(n) because they walk the nodes. ArrayDeque has no index access at all, which is fine for stack and queue use.

Java
new ArrayDeque<String>().offer(null); // NullPointerException
new LinkedList<String>().offer(null); // true
new LinkedList<String>().poll(); // null: empty, or a stored null? we cannot tell

4The Rare Case Where LinkedList Wins

LinkedList's real strength is removing or inserting in the middle at a position we have already reached. With a ListIterator, remove() and add() at the current position are O(1), since they only relink neighbouring nodes. Removing from the middle of an ArrayDeque or an ArrayList shifts elements.

In practice, this matters less than it seems. Reaching the position is usually an O(n) walk anyway, and an ArrayList removeIf, which compacts the array in one pass, is often faster than a LinkedList iteration because of locality. Cases that truly need O(1) removal by position, such as an LRU cache, usually keep a map from keys to nodes of their own linked list, which is what LinkedHashMap does, rather than using java.util.LinkedList.

Java
List<Integer> nums = new LinkedList<>(List.of(1, 2, 3, 4, 5, 6));
ListIterator<Integer> it = nums.listIterator();
while (it.hasNext()) {
if (it.next() % 2 == 0) it.remove(); // O(1) per removal at the current node
}
nums; // [1, 3, 5]

5Replacing the Legacy Stack Class

java.util.Stack dates from Java 1.0. It extends Vector, so every method is synchronized, which adds locking cost even in single-threaded code, and it inherits Vector's index methods, so callers can insert into the middle of a "stack". Its iteration order is from bottom to top, which surprises people. The Javadoc of Stack itself recommends using a Deque instead.

Java
Stack<Integer> old = new Stack<>(); // synchronized, extends Vector
old.push(1); old.push(2);
old.get(0); // 1: index access into a stack
Deque<Integer> stack = new ArrayDeque<>(); // preferred
stack.push(1); stack.push(2);
stack.pop(); // 2

Declaring the variable as Deque<Integer> keeps the code tied to the stack operations and lets us swap the implementation later. If a stack or queue must be shared between threads, the right replacement is a concurrent class such as ConcurrentLinkedDeque or a BlockingQueue, not a synchronized legacy one.

6Follow-up questions

Is ArrayDeque thread safe?

No. Like ArrayList, it has no synchronisation, and concurrent modification can corrupt it. For concurrent use we pick ConcurrentLinkedDeque or ConcurrentLinkedQueue for non-blocking access, or ArrayBlockingQueue and LinkedBlockingQueue when producers and consumers should wait.

Does ArrayDeque ever shrink?

No. Its array grows when needed but never shrinks, so a deque that once held 10 million elements keeps that capacity until it becomes garbage. For a long-lived queue with rare spikes, we can replace it with a new instance after a spike if memory matters.

Why is LinkedList's node 24 bytes?

With compressed pointers, an object header takes 12 bytes, and the node has three references, item, next and prev, of 4 bytes each, for 24 bytes, already a multiple of 8. Without compressed pointers, the header is 16 bytes and each reference 8, so 40 bytes.

7Conclusion

In this answer, we saw that ArrayDeque stores elements in a circular array with head and tail indexes, giving amortized O(1) operations at both ends with no per-element allocation and good cache locality, while LinkedList allocates a 24-byte node per element, adding memory, GC work and cache misses. ArrayDeque rejects null because poll and peek use null for empty. LinkedList is only justified for null elements or O(1) removal at a position held by a ListIterator, and the legacy synchronized Stack should be replaced by ArrayDeque behind the Deque interface.

What to say in the interviewAbout 70 seconds aloud

Open with

Both give O(1) operations at the ends, so the difference is memory layout. ArrayDeque keeps elements in one circular array with head and tail indexes that wrap around, growing the array occasionally, so operations are amortized O(1), there is no allocation per element, and consecutive elements sit next to each other in memory, which the CPU cache and prefetcher love.

Then, if they want more

LinkedList allocates a node for every element, about 24 bytes with compressed pointers, holding the item and prev and next pointers. That means more memory, a million short-lived objects for the GC if we push a million messages, and a likely cache miss for each step through the list.

Two behavioural differences: ArrayDeque rejects null, since poll and peek return null to mean empty, while LinkedList allows it. And LinkedList implements List, but its indexed get is O(n). The only real edge of LinkedList is O(1) insert or remove at a position you already hold through a ListIterator, which is rarely what you need.

I would also replace java.util.Stack, which extends Vector and synchronises every call, with an ArrayDeque declared as a Deque.

About this answer

Last updated
12 November 2026
Versions it names
Java 9
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Why does ArrayDeque usually outperform LinkedList as a stack or queue in Java? Explain...": https://scrivio-interview-library.netlify.app/java/algorithms-and-data-structures/fd1c9e3f3d/