Circular Linked List in JAVA

What is Circular Linked List in JAVA?

Circular linked list is a variation of linear data structures known as linked list. Every element in a linked list is an object and every object has two parts : the data and a reference to the next linked object(node). A linked list isn’t stored at a contagious location but the reference part of one node helps to identify the next node in the list. The referencing is done using pointers. The last node points to the address of null. Head is the first reference that points to the element of the linked list.

  • Circular linked lists on the other hand have eliminated the concept of null.
  • The head points to the very first element and the first node points to the second and the chain continues.
  • The last node instead of pointing towards null, points to the first node making the linked list complete a imaginary circle.
Circular Linked List Introduction in JAVA

Why add on another data structure and increase the hazzle?

Singly linked and doubly linked lists were easy to traverse through. While you cannot go back in a singly linked list, doubly linked list took a little more time when the first element was to be reached from the last element. Circular linked lists on the other hand are very easy to traverse through and make it easier for programmers to get results.

Circular linked List in JAVA

Implementation of Circular Linked List in JAVA

public void addNode(int value) {
    Node newNode = new Node(value);
    if (head == null) {
        head = newNode;
    } else {
        tail.nextNode = newNode;
    tail = newNode;
    tail.nextNode = head;

Merits and Demerits of a circular linked list-

Merits –

1. Any node can be accessed with easy in a circular linked list.
2. Some coding problems involve a circular for which circular linked lists are optimal choice.
3. Queues and advanced data structures can be implemented using circular linked list.

Demerits –

1. Insertion of node in the beginning of the list is a little time consuming since it involves searching of last node to update the reference address.

2. Loop control isn’t easy.

Operation on A Circular Linked List

Code of Circular Linked List in JAVA

    public class Main {  
        public static void main(String[] args) {  
            Main Obj = new Main();  
        public class Node{  
            int element;  
            Node next;  
            public Node(int element) {  
                this.element = element;  
        public Node head = null;  
        public Node tail = null;  
        public void add(int element){  
            Node newEle = new Node(element);  
            if(head == null) {  
                head = newEle;  
                tail = newEle;  
       = head;  
            else {  
       = newEle;  
                tail = newEle;  
       = head;  
        public void print() {  //print function
            Node current = head;  
            if(head == null) {  
                System.out.println("List is empty");  
            else {  
                System.out.println("Nodes of the circular linked list: ");  
                    System.out.print(" "+ current.element);  
                    current =;  
                }while(current != head);  
Nodes of the circular linked list: 
 11 22 33 44

2 comments on “Circular Linked List in JAVA”

  • Mahi

    Woah! Finally got a site that explains it all so clearly. I always used to get stuck while using circular lists, finally understood it perfectly!