Circular 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
- ✓Circular Linked List runs in O(1) insert time and O(n) space, in Java as in every other language.
- ✓No circular list is exposed, but java.util.ArrayDeque is internally a circular array: it keeps head and tail indices into a power-of-two buffer and wraps them with a bitmask.
- ✓Use an array with (i + 1) % n for a fixed-capacity ring, or ArrayDeque when you want the double-ended operations and do not care that the circularity is an implementation detail.
- ✓The Java trap: ArrayDeque forbids null elements and throws NullPointerException if you add one, precisely because null is its internal empty-slot sentinel.
- ✓ArrayDeque rounds capacity up to a power of two so the wrap is a mask rather than a modulo, and the elements sit in one contiguous Object array, which is why it outperforms LinkedList for every queue and stack workload.
What Circular Linked List actually is
Circular Linked List in Java: what you actually reach for
Java implementation
import java.util.ArrayDeque;
import java.util.Collections;
import java.util.Deque;
import java.util.List;
public final class RoundRobin<T> {
// ArrayDeque is a circular array underneath: head and tail indices
// into a power-of-two buffer, wrapped with a bitmask.
private final Deque<T> ring;
public RoundRobin(List<T> workers) {
// Rejects null elements: null is the internal empty-slot sentinel.
this.ring = new ArrayDeque<>(workers);
}
public T next() {
T head = ring.pollFirst();
if (head == null) {
throw new IllegalStateException("empty");
}
ring.addLast(head); // the rotation
return head;
}
// For a plain List, the standard library rotates in place.
public static <T> void rotateInPlace(List<T> items, int distance) {
Collections.rotate(items, distance);
}
// Fixed-capacity ring over an array: no nodes, no boxing for the
// index arithmetic, and a mask instead of a modulo.
public static final class RingBuffer {
private final int[] buf;
private final int mask;
private int head;
public RingBuffer(int capacityPowerOfTwo) {
if (Integer.bitCount(capacityPowerOfTwo) != 1) {
throw new IllegalArgumentException("capacity must be a power of two");
}
this.buf = new int[capacityPowerOfTwo];
this.mask = capacityPowerOfTwo - 1;
}
public void push(int v) {
buf[head] = v;
head = (head + 1) & mask; // mask, not %
}
}
}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 Circular Linked List?
No circular list is exposed, but java.util.ArrayDeque is internally a circular array: it keeps head and tail indices into a power-of-two buffer and wraps them with a bitmask. Collections.rotate rotates any List in place, which covers the round-robin case without a custom structure.
What is the most common Java mistake when implementing Circular Linked List?
ArrayDeque forbids null elements and throws NullPointerException if you add one, precisely because null is its internal empty-slot sentinel. That makes a migration from LinkedList, which permits nulls, a source of run-time failures in exactly the edge cases your tests skip. If you do build a node ring, remember that a for-each loop over it never terminates, and that any hand-written toString or equals will recurse until the stack overflows.
How does Circular Linked List actually perform in Java?
ArrayDeque rounds capacity up to a power of two so the wrap is a mask rather than a modulo, and the elements sit in one contiguous Object array, which is why it outperforms LinkedList for every queue and stack workload. Boxing still applies for primitive payloads. 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? Student discounts on developer tools are collected on Noizz, tool by tool.
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.