In Java, a LinkedList is a data structure that increases its size when the elements are added to it and shrinks its size when the elements are removed from it. The implementation of a LinkedList in Java involves certain operations like insertion, deletion, and other operations that are quite easy to perform.
Among different types of LinkedLists, one is a circular LinkedList that connects each node in such a way that the last node links with the first node.
This article will demonstrate how to create a Circular LinkedList and display it in Java.
How to Create and Display a Circular LinkedList in Java?
A circular LinkedList in Java is a type of LinkedList in which both the last and the first nodes are interconnected with each other. The connected nodes thus form a circle. The circular linked list consists of two major parts. The data is in the first part of the CircularLinkedList and the second part points to the next node. Since The LinkedList is circular therefore the last node points to the head of the declared list.
To understand it better, first, we will create a circular LinkedList and then will display it.
Step 1: Create a Circular LinkedList in Java
The code below depicts how to create a circular LinkedList in Java.
//Declare a class for nodes of the linked list
class circularlist {
//Indicates the nodes present in the list.
public class nodes{
int entry;
nodes next;
public nodes(int entry) {
this.entry = entry;
}
}
//Head pointer in the list is declared null.
public nodes head = null;
//tail pointer in the list is declared null
public nodes tail = null;
//Function to add a node at the end of the list.
public void add(int entry){
nodes freshnode = new nodes(entry);
//This statement checks for an empty list.
if(head == null) {
//head and tail will point to a fresh node for an empty list.
head = freshnode;
tail = freshnode;
//The fresh node will correspond to the next node.
freshnode.next = head;
}
else {
//tail here indicates the new node
tail.next = freshnode;
//Tail will indicate the new node
tail = freshnode;
//pointing to the head for a circular linked list
tail.next = head;
}
}
In the above code:
- A class named “circularlist” is created.
- In the node class, the head node and the tail node are initialized as null.
- A fresh node is created and added to the tail and head of the circular linked list.
- Since it is circular in nature, therefore, the next node of the Linked List points to the head.
Step 2: Display a Circular LinkedList in Java
The display() method in Java displays all the nodes in a circular linked list.
//The Display() method to display all the nodes present
public void display() {
nodes current = head;
//If the head of the list is null.
if(head == null) { //Print, the List is empty
System.out.println("The present list is empty");
}
else {
System.out.println("The nodes present in a circular linked lists are: ");
do{
System.out.print(" "+ current.entry);
current = current.next;
}while(current != head);
System.out.println();
}
}
//main() method in Java
public static void main(String[] args) {
//Using the add() method to insert the elements to the list.
circularlist a = new circularlist();
a.add(1);
a.add(2);
a.add(3);
a.add(4);
a.add(5);
a.add(9);
a.display();
}
}
In the above code:
- The if statement declares the list as empty if the head is null.
- The do-while loop helps in printing the nodes in the circular LinkedList.
- The next step involves creating an object for a calling function.
- The add() method adds the nodes and the display() method displays the nodes.
Output
In the below output, the nodes in the circular linked list are printed.

This concludes the method to create and display a circular LinkedList in Java.
Conclusion
A circular LinkedList in Java is a type of LinkedList that connects each node in such a way that the tail node links with the head node. The connected nodes form a circle. The circular linked list in Java can be created using the traditional do-while loop. In this article, we have demonstrated the ways to create and display a circular LinkedList in Java.