Please login


Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

Insertion at the End of a Circular Linked List in JAVA

Insertion at the end of a circular linked list

JAVA Program to Insert a Node in the end of a Circular Linked List

Circular linked list is a very flexible data structure where you can traverse through it without it being at a random location. Inserting a node at the end of the circular linked means replacing the address of the last node with the address of new node and then making the new node point towards the head node.

Given below is the description of how you can insert a node at the End of a Circular Linked List

There are can be two conditions here –

  • The circular linked list is empty –
    • If the circular linked is empty which means the head is null then the new node will be added as head.
    • Both the head and the tail will point towards the head because there is only one element.

  • The circular linked list is not empty –
    • A head is the first node of the circular linked list which can be considered as the starting of the list and points towards the next element.
    • The tail is the last element of the circular linked list and it point towards the head making the list appear circular.
    • If the list is not empty which means if the list has elements the last node will be the tail.
    • To insert an element in the end of circular linked list, the present tail should be replaced by the new node.
    • By changing the node referred by the current tail to the newly added node, it has become the new tail.
    • Now, make the new tail point towards the head of the circular linked list and the element is added in the end of the circular linked list.

The following Algorithm is to be followed in the Program below

  • addLast(element)
  • IF Head==Null
    • Head -> newNode
    • Tail -> newNode
    • newNode->next = Head
    • EXIT
  • ELSE
    • tail->next = newNode
    • tail = newNode
    • tail->next = Head
    • EXIT

Code for insertion 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 print() {  
        Node current = head;    
        if(head == null) {  
            System.out.println("Empty List");  
        else {  
            System.out.println("After adding node at the rear end");  
                //Prints each node by incrementing pointer.  
                System.out.print(" "+ current.element);  
                current =;  
            }while(current != head);  
    public void addLast(int element){  
        Node newNode = new Node(element);  
        if(head == null) {   //For a empty list
            head = newNode;  
            tail = newNode;  //For a empty list tail and head both will have the refernce to the new node
   = head;  
        else {  
   = newNode;  //tail will have the reference to the new node.
            tail = newNode;  //New node will become new tail.
   = head;  //In circular linked list tail will alaways point to head.
After adding node at the rear end
After adding node at the rear end
 1 2
After adding node at the rear end
 1 2 3
After adding node at the rear end
 1 2 3 4