Circular Linked List Implementation in Java
Last Updated :
15 May, 2024
A Circular Linked List is a variation of the traditional linked list data structure. In the traditional linked list, the last node points to the null and it can indicating the end of the list. However, in the circular linked list, the last node points back to the first node and forms the circle or loop. This circular structure allows for the efficient traversal and manipulation of the elements in the list.
In this article, we will learn to implement a Circular Linked List in Java.
Organization of the Circular Linked List Data Structure
In the Circular Linked List, each node contains two fields and there are one is data and the other is a reference to the next node in the sequence. Unlike in the traditional linked list, the last node's next reference points back to the first node and it can create a circular structure.

Implementation
The Circular Linked List can implemented using the Node class to represent each element in the list and the LinkedList class to manage the list itself. The LinkedList class can contain the methods for adding, removing, and traversing in the list.
Basic Operations
- Insertion: Inserting the node at the beginning or the end of the list can be done on the Circular LinkedList in Java.
- Deletion: Deleting the node at the beginning or the end of the list of the Circular LinkedList.
- Traversal: Traversal can define the traversing of the entire list of the Circular LinkedList.
Operations Algorthims:
Operations
| Algorithms
| Complexity
|
---|
1.Insert at the Begining
| - Create the new node with given data.
- If list is empty, set the both head and tail to new node and make its the next pointer point to itself.
- Otherwise, set the new node next pointer to current head and update the head to new node and make the tail next pointer point to new head.
| Time Complexity: O(1) Space Complexity: O(1)
|
---|
2. Insertion at the End
| - Create the new node with given data.
- If list is empty, set the both head and tail to the new node and make its the next pointer point to itself.
- Otherwise, Set the current tail next pointer to new node and update the new node and make the new trail next pointer to head.
| Time Complexity: O(1) Space Complexity: O(1)
|
---|
3. Deletion at the Beginging
| - If list is empty, return
- If list has only one node then set the both head and tail to the null.
- Otherwise, update the head to its next node and make the tails next pointer point to new head.
| Time Complexity: O(1) Space Complexity: O(1)
|
---|
4. Deletion ar the end
| - If the list is empty, return
- if list has only one node then set the both head and tail to the null.
- Otherwise, traverse the list to the find the node before the tail and update the tail to this node and it can make the new tail next pointer point to the head.
| Time Complexity: O(1) Space Complexity: O(1)
|
---|
Traversal
| - If list is empty the print "List is Empty" and return.
- Start from the head and it can iterate through the list and printing the each node in list once.
| Time Complexity: O(n) Space Complexity: O(1)
|
---|
Steps to Implementation of Cirular LinkedList
- Define the node class: Create the class to represents the each element(node) in the circular Linked List and each node can contains the data and the reference to the next node.
- Define the CircularLinkedList class: Create the class to manage the Circular Linked List and this class can contains the methods for the insertion, deletion, traversal and any other necessary operations.
- Implements the insertion methods: We can add the methods to the insert the nodes at the beginging and end of the list. Ensure that next pointer of the last node always points back to the head to maintains the circular structure.
- Implements the deletion methods: We can add the methods to delete the nodes from the begining and the end of list and it can update the head and tail pointers.
- Implements the traversal: We can add the method to the traverse the list and display its the elements of the list.
- Finally, We can write the main method to create the instance of CirularLinkedlist class and implements the performing the various operations such as the insertion, deletion and traversal of the Circular Linked List.
Example Program:
Java
// Java Program for Implementing
// Circular Linked List
// Class Node
class Node {
int data;
Node next;
// Constructor to initialize a node with data
public Node(int data) {
this.data = data;
this.next = null;
}
}
// Class CircularLinkedList
class CircularLinkedList {
private Node head;
private Node tail;
// Constructor CircularLinkedList
public CircularLinkedList() {
head = null;
tail = null;
}
// Method to insert a new node with the given
// data into the circular linked list
public void insert(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 the node with the given
// key from the circular linked list
public void delete(int key) {
if (head == null) {
return;
}
Node curr = head;
Node prev = null;
while (curr.next != head) {
if (curr.data == key) {
// If the node to be deleted is the head node
if (prev == null) {
Node last = head;
while (last.next != head) {
last = last.next;
}
head = curr.next;
last.next = head;
return;
} else {
prev.next = curr.next;
// Update tail if the last node is deleted
if (curr == tail) {
tail = prev;
}
return;
}
}
prev = curr;
curr = curr.next;
}
// Check if the node to be deleted is the last node
if (curr == head && curr.data == key) {
prev.next = head;
// Update tail if the last node is deleted
tail = prev;
}
}
// Method to display the elements
// of the circular linked 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();
}
}
public class MyClass {
public static void main(String[] args) {
// Create a circular linked list
CircularLinkedList list = new CircularLinkedList();
// Insert elements into the list
list.insert(1);
list.insert(2);
list.insert(3);
list.insert(4);
// Print the list
// Output: 1 2 3 4
list.printList();
// Delete an element from the list
list.delete(2);
// Print the updated list
// Output: 1 3 4
list.printList();
}
}
OutputLinked List Elements : 1 2 3 4
Linked List Elements : 1 3 4
Complexity of the Above Method:
Time Complexity:
- The overall time complexity for the most operations like insertion at begining, insertion at end, deletion at begining and traversal is O(1) or O(n) where the n is number of the element in list.
- Insertion and deletion operations at the begining and end of list have the constant time complexity is O(1) as they can involves the fixed number of operations regardless of list size.
- Deletion at end and traversal operations have the linear time complexity is O(n) as they can require the traversing the entire list which grows with the number of the elements in list.
Space Complexity:
- Overall space complexity of the circular Linked List implementation is O(n) where n is the number of the elements in list.
- This space complexity can required to stores the node of linked list.
- The space complexity of the individual methods is constant O(1) as they can required only fixed amount of memory for the variables and temporary nodes of the list.
Applications of Circular Linked List
- It can applies in Circular Queues
- It can used to implementation of the Round-Robin scheduling algorithms
- It can used to developed the music playlist management
Similar Reads
Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s
10 min read
Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac
15+ min read
Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
13 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt
10 min read
Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its
8 min read