Circular Linked List Insertion in the middle in Java

Java Program to Insert in the middle in Circular Linked List

We will be looking at a couple of methods to Do Insertion in the middle of a Circular Linked List in Java.

First we need to find the middle of the linked list then insert a node.

insert in the middle of circular linked list

Approaches Discussed

  • Approach 1: Using Size variable to keep track of the size of Circular Linked List
  • Approach 2: Using Function to calculate size of Circular Linked List at any point in time
Circular Linked List Insertion in the middle in Java
public void insertMiddle(int data) {
        Node newNode = new Node(data);

        // if the LL was empty
        if(head == null){
            // use insert function to insert
            insert(data);
            return;
        }

        // otherwise
        Node temp = head;

        // find correct insertion position for middle
        int mid = (size % 2 == 0) ? (size/2) : (size+1)/2;

        // Unique case when there is only one node in CLL
        // we will be inserting at the last,
        // inserting 2nd node at the last
        // Example size = 1 will result in mid = 1 so entering after 1st node
        // where size itself is 1 so entering last node
        if(mid == size){
            newNode.next = head;
            head.next = newNode;
            return;
        }
        // traverse to current mid position
        while(--mid > 0){
            temp = temp.next;
        }

        newNode.next = temp.next;
        temp.next = newNode;
        size++;
    }

Code for Sorted Insert In 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