Doubly Linked List Implementation in Java
14-day free trial
Try SeekerPro free for 14 days.
Full access while you try it · then $15.99/mo · cancel anytime
Start your 14-day free SeekerPro trial →30-day money-back guarantee · Cancel anytime
Not checking a site today? Turn this topic into a ready to paste AI prompt, free →
Key Takeaways
- ✓Doubly Linked List runs in O(1) insert time and O(n) space, in Java as in every other language.
- ✓java.util.LinkedList<E> implements both List and Deque and is a genuine doubly linked list.
- ✓Use ArrayDeque when you want a stack or queue, because it is faster and produces less garbage, and reserve LinkedList for the case where you are splicing through a ListIterator.
- ✓The Java trap: list.get(i) is O(n) but reads exactly like an array access, so the ordinary for (int i = 0; i < list.size(); i++) loop over a LinkedList is silently O(n squared).
- ✓Each node is a distinct object: a 12 to 16 byte header plus three references, so roughly 40 bytes to store one element, against 4 to 8 bytes per slot in an ArrayList.
What Doubly Linked List actually is
Doubly Linked List in Java: what you actually reach for
Java implementation
import java.util.LinkedList;
import java.util.ListIterator;
public final class Playlist {
private final LinkedList<String> tracks = new LinkedList<>();
// The ONE thing LinkedList does better than ArrayList: O(1) insertion
// and removal at a cursor you are already holding.
public void insertAfterFirstMatch(String needle, String track) {
ListIterator<String> it = tracks.listIterator();
while (it.hasNext()) {
if (it.next().equals(needle)) {
it.add(track); // O(1) at the cursor, no shifting
return;
}
}
}
public void dropEveryOther() {
ListIterator<String> it = tracks.listIterator();
boolean drop = false;
while (it.hasNext()) {
it.next();
if (drop) {
it.remove(); // O(1), and safe: no ConcurrentModificationException
}
drop = !drop;
}
}
// Deliberately NOT written as: for (int i = 0; i < tracks.size(); i++) tracks.get(i)
// That loop is O(n^2) on a LinkedList and looks identical to the ArrayList version.
public int totalLength() {
int total = 0;
for (String t : tracks) { // iterator-based: O(n)
total += t.length();
}
return total;
}
}Think Your Code Is Clean? Let NexusBro QA It in 20 Seconds.
Paste your code. Click QA. Get an instant expert-level audit with fixes.
QA My Code FreeThe Java-specific trap
Performance in Java, beyond the O(1) insert
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which Java type or library should I use for Doubly Linked List?
java.util.LinkedList<E> implements both List and Deque and is a genuine doubly linked list. The node type is package-private, so the only way to reach the O(1) middle insertion the structure exists for is through ListIterator, whose add and remove operate at the cursor in constant time.
What is the most common Java mistake when implementing Doubly Linked List?
list.get(i) is O(n) but reads exactly like an array access, so the ordinary for (int i = 0; i < list.size(); i++) loop over a LinkedList is silently O(n squared). It is the single most common performance bug of this class in Java, and it is invisible in a unit test with ten elements. Two further traps: LinkedList permits null elements while ArrayDeque forbids them, so the migration between them is not mechanical, and the iterators are fail-fast, throwing ConcurrentModificationException if you structurally modify the list during a for-each rather than through the iterator itself.
How does Doubly Linked List actually perform in Java?
Each node is a distinct object: a 12 to 16 byte header plus three references, so roughly 40 bytes to store one element, against 4 to 8 bytes per slot in an ArrayList. The nodes are scattered across the heap, so traversal is a chain of cache misses, and the extra objects add real pressure to the young generation. Time complexity is O(1) insert and space is O(n).
Related Articles
Rather try before you buy?
Get the rest of the vault: every cheat sheet, guide and audit in SeekerPro, free for 14 days, then $15.99/mo. Cancel anytime. Daily usage limits apply.
Start my 14-day SeekerPro trialStudying for this? Devin free tiers and academic plans are laid out on Noizz.
Is your site built to last?
Run a free QA audit and get your Site Health Score in seconds.
Check Your Site FreeNo signup required
Reader comments
No account needed. Share what worked, what did not, and what you would add.
Add your take below. A concrete tip, a result or a question helps the next reader most.