Insertion at the nth node of a Circular Linked List

how to prepare for placements for eee freshers

JAVA Program to Insert a Node at the nth node of a Circular Linked List

It is very easy to insert nodes in a circular linked list because every node is pointing towards another node and they are not at a contagious location. No shifting of elements is required for the insertion to happen. For insertion at the nth node of a Circular linked list, the address the (n-1) th node is pointing to should be changed to the address of the new node and the new node should point to the node (n-1)th node was pointing previously pointing towards.

Insertion in Between The Nodes in Circular Linked List in java

Given below is the description of Insertion at the nth Node of a Circular Linked List 

Insertion at nth position in a circular linked list –

  • While inserting a new node in the anywhere between the head and the rails we need to consider the three nodes.
  • The (n-1)th node must be pointing towards the current nth node, the current nth node must be pointing towards the current (n+1)th node.
  • First of all the node (n-1)th address of the next node must be copied in the address part of the new node. Since it is referring to the node we will consider as n+1 after the insertion is successful.
  • Change the address of (n-1)th node to the address of the new node.
  • In this way, insertion at any position can be done in a circular linked list.
public void insertAfter(int n,int data)
    {
        int size = calcSize(head);

        // Can only insert after 1st position
        // Can't insert if position to insert is greater than size of Linked List
        if(n < 1 || n > size)
        {
            System.out.println("Can't insert\n");

        }
        else
        {
            Node newNode = new Node(data);
            // required to traverse
            Node temp = head;

            // traverse to the nth node
            while(--n > 1)
                temp=temp.next;

            newNode.next= temp.next;
            temp.next = newNode;
        }
    }
    public int calcSize(Node node){
        int size = 0;
        while(node!=tail){
            node = node.next;
            size++;
        }
        return size+1;
    }

Algorithm for insertion at nth node of a circular linked list

  • 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 at Nth node of a circular Linked List in Java

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Circular Linked List

  • Introduction to Circular Linked List
    Click Here
  • Circular Linked List Applications
    Click Here
  • Circular Linked List in –
    C | C++ | Java
  • Insertion in Circular Linked List –
    C | C++ | Java
  • Insertion at the beginning–
    C | C++ | Java
  • Insertion at the end –
    C | C++ | Java
  • Insertion at nth position –
    C | C++ | Java
  • Deletion in Circular Linked List –
    C | C++ | Java
  • Deletion from beginning in Circular Linked List –
    C | C++ | Java
  • Deletion from nth position in Circular Linked List –
  • Deletion from end in Circular Linked List –
    C | C++ | Java
  • Insertion and Deletion in Circular Linked List – C | C++ | Java
  • Split a Circular Linked List in two halves –
    C | C++ | Java
  • Count nodes in Circular Linked List –
    C | C++ | Java
  • Sorted Insert In Circular Linked List –
    C | C++ | Java
  • Insertion in the middle in Circular Linked List –
    C | C++ | Java

Circular Linked List