A Circular Linked List is almost the same as a singly linked list, but with one important difference: the last node does not point to null. Instead, it points back to the first node of the list.
This creates a circle, which makes continuous traversal very easy. In this chapter, we will implement a circular linked list from scratch in Java. We will learn how to insert nodes, delete nodes, search values, and loop through the list without going into an infinite loop.
This is a very important concept in data structures and helps in understanding circular queues, round-robin scheduling, and other repeating systems.
Where Circular Linked Lists Used?
- Music players (continuous loop playlist)
- CPU Scheduling (Round Robin Algorithm)
- Multiplayer games (turn-based systems)
- Circular queues
- Traffic light control system
Circular Linked List Implementation in Java
In the Circular Linked List Java code below, we will implement:
- Insert at the beginning
- Insert at the end
- Delete first
- Delete last
- Search for a value
- Traverse
// Node class
class Node {
int data;
Node next;
Node(int data) {
this.data = data;
this.next = null;
}
}
// Circular Linked List Implementation
public class CircularLinkedList {
private Node last;
private int size;
// Insert at beginning
public void addFirst(int value) {
Node newNode = new Node(value);
if (last == null) {
last = newNode;
last.next = last; // circular
} else {
newNode.next = last.next;
last.next = newNode;
}
size++;
}
// Insert at end
public void addLast(int value) {
Node newNode = new Node(value);
if (last == null) {
last = newNode;
last.next = last;
} else {
newNode.next = last.next;
last.next = newNode;
last = newNode; // update last
}
size++;
}
// Delete first node
public void removeFirst() {
if (last == null) {
System.out.println("List is empty");
return;
}
if (last.next == last) { // only one node
last = null;
} else {
last.next = last.next.next; // skip first node
}
size--;
}
// Delete last node
public void removeLast() {
if (last == null) {
System.out.println("List is empty");
return;
}
if (last.next == last) { // single node
last = null;
size--;
return;
}
Node temp = last.next;
// move to second-last node
while (temp.next != last) {
temp = temp.next;
}
temp.next = last.next;
last = temp;
size--;
}
// Search a value
public int search(int value) {
if (last == null) return -1;
Node temp = last.next;
int index = 0;
do {
if (temp.data == value) return index;
temp = temp.next;
index++;
} while (temp != last.next);
return -1;
}
// Traverse list
public void printList() {
if (last == null) {
System.out.println("List is empty");
return;
}
Node temp = last.next;
do {
System.out.print(temp.data + " -> ");
temp = temp.next;
} while (temp != last.next);
System.out.println("(back to start)");
}
// Main method to test
public static void main(String[] args) {
CircularLinkedList cll = new CircularLinkedList();
cll.addLast(10);
cll.addLast(20);
cll.addFirst(5);
cll.addLast(30);
System.out.print("Circular List: ");
cll.printList(); // 5 -> 10 -> 20 -> 30 -> (back to start)
cll.removeFirst();
cll.removeLast();
System.out.print("After Deletions: ");
cll.printList(); // 10 -> 20 -> (back to start)
int pos = cll.search(20);
if (pos != -1) {
System.out.println("20 found at index: " + pos);
} else {
System.out.println("Value not found");
}
}
}
Think of people standing in a circle and passing a ball to the person on their right. There is no end – the ball keeps moving in a cycle. A circular linked list works in a very similar way.