LinkedList as Deque in Java
Fast recall
Quick revision
Core answer
LinkedList keeps head and tail node references, so addFirst, addLast, removeFirst, and removeLast are all O(1) — making it a natural Deque usable as a queue (FIFO) or stack (LIFO).Awareness that LinkedList serves a dual purpose as both a List and a Deque, and understanding of queue/stack data structures
Only knowing LinkedList as a List implementation without realizing it can be used as a queue, stack, or double-ended queue
Mention that ArrayDeque is typically preferred over LinkedList for pure queue/stack operations due to better cache locality and lower memory overhead
Interview-ready guide
Interview answer
Deque stands for double-ended queue, pronounced like deck.
It's an interface that lets you add and remove elements from both the front and the back of the collection, and all those operations should be O of 1. LinkedList implements Deque because it has references to both the head node and the tail node of the linked list. That means addFirst, addLast, removeFirst, and removeLast are all just pointer updates — no traversal needed.
You can use LinkedList as a FIFO queue by adding at the tail with addLast and removing from the head with removeFirst. Or you can use it as a LIFO stack by adding and removing from the same end, like addFirst and removeFirst — that's your push and pop.
The Deque interface is really useful for things like job queues where high-priority tasks need to jump to the front, or undo-redo stacks where you push and pop actions. One thing to know is that while LinkedList works as a Deque, in most production code you'd actually see ArrayDeque instead.
ArrayDeque is backed by a circular array, so it still has O of 1 operations at both ends, but it's faster in practice because the array elements are next to each other in memory, which plays well with CPU caches. LinkedList nodes are scattered all over the heap.
So you'd pick LinkedList over ArrayDeque mainly when you need both the List interface for random access by index and the Deque interface for the end operations. If you just need a queue or stack, ArrayDeque is the better choice.
LinkedList as Deque in Java
Direct Answer
A Deque (double-ended queue) supports efficient insert/remove at both ends. LinkedList keeps head and tail node references, so addFirst, addLast, removeFirst, and removeLast are all O(1) — making it a natural Deque usable as a queue (FIFO) or stack (LIFO).
Code Example
import java.util.Deque;
import java.util.LinkedList;
public class LinkedListDequeDemo {
public static void main(String[] args) {
// FIFO queue
Deque<String> orderQueue = new LinkedList<>();
orderQueue.addLast("Order-101"); // O(1) enqueue at tail
orderQueue.addLast("Order-102");
String processed = orderQueue.removeFirst(); // O(1) dequeue at head
// LIFO stack
Deque<String> undoStack = new LinkedList<>();
undoStack.addFirst("Action-1"); // O(1) push at front
undoStack.addFirst("Action-2");
String lastAction = undoStack.removeFirst(); // O(1) pop at front
// Double-ended queue
Deque<String> taskDeque = new LinkedList<>();
taskDeque.addFirst("High-priority"); // urgent at front
taskDeque.addLast("Low-priority"); // regular at back
String urgent = taskDeque.removeFirst();
String regular = taskDeque.removeLast();
}
}
Common Mistakes & Pitfalls
- Not knowing
LinkedListcan be a queue: Many freshers learnArrayListandLinkedListpurely asListimplementations and never realizeLinkedListimplementsDeque, so they reinvent queue logic with manual index math. - Using the legacy
Stackclass: Java's oldStackis a synchronizedVectorsubclass and is obsolete — useDequewithaddFirst/removeFirst, ideally viaArrayDeque. - Picking
LinkedListby default for queues: It works as aDeque, butArrayDequeis usually faster thanks to cache behaviour — chooseLinkedListonly when you need the combinedListplusDequeinterface.
Check your understanding
Answer these without looking back at the article.
- When would you choose ArrayDeque over LinkedList?
- What is the difference between Queue and Deque?
- What methods does LinkedList have that ArrayList doesn't?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.