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.
Mini Quiz
Mini Project
Mini Project: Continuous Music Playlist using Circular Linked List
Question:
A music player can play songs continuously in a loop. After the last song finishes, it starts playing the first song again. Create a Java program to implement a Continuous Music Playlist using a Circular Linked List.
Perform the following operations:
- Add a song at the beginning of the playlist.
- Add a song at the end of the playlist.
- Remove the first song.
- Remove the last song.
- Search for a song ID.
- Display all songs in the playlist in circular order.
View Answer Code
// Node class
class Node {
int data;
Node next;
Node(int data) {
this.data = data;
this.next = null;
}
}
// Circular Linked List for Music Playlist
public class MusicPlaylist {
private Node last;
private int size;
// Add song at the beginning
public void addFirst(int value) {
Node newNode = new Node(value);
if (last == null) {
last = newNode;
last.next = last;
} else {
newNode.next = last.next;
last.next = newNode;
}
size++;
}
// Add song at the 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;
}
size++;
}
// Remove first song
public void removeFirst() {
if (last == null) {
System.out.println("Playlist is empty");
return;
}
if (last.next == last) {
last = null;
} else {
last.next = last.next.next;
}
size--;
}
// Remove last song
public void removeLast() {
if (last == null) {
System.out.println("Playlist is empty");
return;
}
if (last.next == last) {
last = null;
size--;
return;
}
Node temp = last.next;
while (temp.next != last) {
temp = temp.next;
}
temp.next = last.next;
last = temp;
size--;
}
// Search song
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;
}
// Display playlist
public void printList() {
if (last == null) {
System.out.println("Playlist 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 first song)");
}
public static void main(String[] args) {
MusicPlaylist playlist = new MusicPlaylist();
playlist.addLast(101);
playlist.addLast(102);
playlist.addFirst(100);
playlist.addLast(103);
System.out.print("Playlist: ");
playlist.printList();
playlist.removeFirst();
playlist.removeLast();
System.out.print("Playlist after removing songs: ");
playlist.printList();
int position = playlist.search(102);
if (position != -1)
System.out.println("Song found at position: " + position);
else
System.out.println("Song not found.");
}
}
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
Shubhranshu Shekhar is a coding instructor, mentor, and founder of VSIT Delhi with 20+ years of teaching experience (since 2004). He has guided many students who are now working in multinational companies and specializes in Full Stack Development, Python, Digital Marketing, and Data Analytics.