A circular linked list is a linked list whose last node links back to the first node, so the nodes form a ring instead of a line. This page covers its two types, its operations and their cost, a Java implementation, and when a circular list is a good fit.
What is a circular linked list?
In a circular linked list, the last node's next refers to the first node instead of being null. There is no natural end: starting from any node and following next, you eventually come back to the same node.
next from any node visits every node and comes back to where it started: 20 → 40 → 60 → 80 → 20 → …Because there is no null to stop at, loops over a circular list must stop when they return to the node where they began. Like other linked lists, the ring grows and shrinks one node at a time.
Types of Circular Linked Lists
Either kind of linked list can be closed into a ring, which gives two types:
1. Circular Singly Linked List
A singly linked list whose last node points back to the first. Each node has only a next link, so the ring can be walked in one direction only.
data and next; tail.next is head, so traversal only moves forward and wraps around.2. Circular Doubly Linked List
A doubly linked list closed at both ends: the last node's next refers to the first node, and the first node's prev refers to the last node. No link is null, and the ring can be walked in both directions.
tail.next is head and head.prev is tail, so you can loop forward (purple) or backward (teal) without ever reaching null.Operations on the Circular Linked List
We can perform the following operations on the Circular Linked List as given below.
- Insertion
- Insert node at the beginning
- Insert node at the end
- Insert node at the specific position
- Deletion
- Delete the first node
- Delete the last node
- Delete node from the specific position
- Searching
- Search a specific node
Time and Space Complexity of Circular Linked List Operations
| Operations | Time Complexity | Space Complexity |
|---|---|---|
| Insertion | O(1) to O(n) | O(1) |
| Deletion | O(1) to O(n) | O(1) |
| Traversal | O(n) | O(1) |
| Search | O(n) | O(1) |
| Access | O(n) | O(1) |
Example of Circular Linked List in Java
Now, let's understand the implementation of circular linked list program in Java.
Class: Node
class Node {
int data; // Store value
Node next; // Store address of next node
// Initializing a node with data in the constructor
public Node(int data) {
this.data = data;
this.next = null;
}
}
Class: CircularLinkedList
class CircularLinkedList {
private Node head;
private Node tail;
// Setting null in head and tail in the constructor
public CircularLinkedList() {
head = null;
tail = null;
}
// Method to insert a new node into the circular linked list
public void insertNode(int data) {
Node newNode = new Node(data);
// If the list is empty, make the new node the head and tail
if (head == null) {
head = newNode;
tail = newNode;
// Point to itself in a circular list
newNode.next = head;
} else {
newNode.next = head;
tail.next = newNode;
tail = newNode;
}
}
// Method to delete an existing node from the circular linked list
public void deleteNode(int key) {
if (head == null) {
System.out.println("List is empty");
return;
}
// prev starts at the tail, the node before head in the circle
Node prev = tail;
Node curr = head;
do {
if (curr.data == key) {
if (head == tail) {
// The only node: the list becomes empty
head = null;
tail = null;
} else {
prev.next = curr.next; // skip the node
if (curr == head) {
head = curr.next; // tail.next is now the new head
}
if (curr == tail) {
tail = prev; // prev.next is still head
}
}
System.out.println("Given key " + key + " deleted successfully.");
return;
}
prev = curr;
curr = curr.next;
} while (curr != head);
System.out.println("Key not found in the list");
}
public void printList() {
if (head == null) {
System.out.println("List is empty");
return;
}
System.out.print("Linked List Elements: ");
Node itr = head;
do {
System.out.print(itr.data + " ");
itr = itr.next;
} while (itr != head);
System.out.println();
}
}
Class: CircularLinkedListOutput
public class CircularLinkedListOutput {
public static void main(String[] args) {
// Create a circular linked list
CircularLinkedList list = new CircularLinkedList();
// Insert elements into the list
list.insertNode(20);
list.insertNode(40);
list.insertNode(60);
list.insertNode(80);
// Print the list
list.printList();
// Delete an element from the list
list.deleteNode(60);
// Delete a non existing element from the list
list.deleteNode(10);
// Print the list
list.printList();
}
}
Linked List Elements: 20 40 60 80 Given key 60 deleted successfully. Key not found in the list Linked List Elements: 20 40 80
When to Use a Circular Linked List?
A circular list fits problems where the items naturally repeat in a cycle:
- Taking turns: round-robin scheduling or players in a game, where after the last item you go back to the first.
- Repeating sequences: a playlist on repeat or a slideshow that loops.
- A queue that only needs one reference: keeping a reference to the last node gives O(1) access to both ends, because the first node is
last.next.
Advantages of Circular Linked List
- Natural cycling: moving past the last node brings you back to the first with no special case.
- Any node can be a starting point: a full traversal can begin anywhere in the ring.
- O(1) access to both ends with one reference: with a reference to the last node, inserting at the front or the back is O(1).
- No fixed capacity: nodes are added and removed one at a time.
Disadvantages of Circular Linked List
- Risk of infinite loops: a loop that waits for
nullnever ends; the stop condition must be "back at the start". - More edge cases: the single-node ring (a node pointing to itself) and the head/tail links must be handled carefully on every insert and delete.
- No clear end: it is a poor fit when the data has a natural first and last element.
- Same access costs as other linked lists: no random access, and in a singly circular list deleting the last node is O(n).
Use Cases of Circular Linked List
- Round-robin CPU or task scheduling.
- Media players that loop a playlist.
- Turn order in multiplayer games.
- The Josephus problem and other "every k-th item" puzzles (see FAQ Q7).
Choose a circular linked list when the problem itself is circular. When the data has a clear beginning and end, an ordinary linked list or an array is simpler and less error-prone.