Java Program to Insert a New Node at the Middle of the Circular Linked List Last Updated : 15 Aug, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a Circular Linked List, the task is to add a New Node at the Middle of the List. Let's consider the following Circular Linked List: List-Before-InsertionList-After-InsertionCreate a new node (New_node).Check for an empty list. If the list is empty then insert the node as head.For non-empty list, calculate the length of the list. Create variable mid and store middle length in it.Create two nodes Temporary and Current. Now traverse the list till Temporary reaches the midpoint of the list using the mid variable.Insert the New_node after the Current.Make Current.next point to New node and New_node.next to Temporary.Node is inserted.Below is the implementation of the above approach: Java // Java Program to Insert a New Node at // the Middle of the Circular Linked List import java.io.*; public class GFG { // Stores Information about Node of List public class Node { int data; Node next; public Node(int data) { this.data = data; } } // Declaring Head of the Node public Node head_of_node = null; // A last pointer to help append values to our list public Node last = null; // keep count of size of the list public int length_of_list; // Add method adds values to the end of the list public void add(int data) { Node newNode = new Node(data); if (head_of_node == null) { head_of_node = newNode; last = newNode; newNode.next = head_of_node; } else { last.next = newNode; last = newNode; last.next = head_of_node; } // keep count of size of list length_of_list++; } // Method to insert Node in the middle of the list public void Insert_In_Middle(int data) { // creating a new node that is to be inserted Node New_node = new Node(data); // check for empty list if (head_of_node == null) { head_of_node = New_node; last = New_node; New_node.next = head_of_node; } // If the list is not empty else { // Declaring nodes Node temporary; Node current; // Checking if the length of list // if even or odd int mid_point_check = (length_of_list % 2); // mid point for even length if (mid_point_check == 0) { length_of_list = length_of_list / 2; } // mid point for odd length else { length_of_list = (length_of_list + 1) / 2; } // temporary points to the head of list temporary = head_of_node; current = null; // loop till we reach the mid point while (length_of_list > 0) { // make current point to the previous node current = temporary; temporary = temporary.next; length_of_list -= 1; } // make the previous node point to the new node current.next = New_node; // make the new_node.next point to temporary New_node.next = temporary; } // increasing the length of list after insertion of // new node length_of_list++; } // Print_list method iterates through the list and // prints the values stored in the list public void Print_List() { Node current = head_of_node; if (head_of_node == null) { System.out.println("Your list is empty"); } else { do { System.out.print(" " + current.data); current = current.next; } while (current != head_of_node); System.out.println(); } } // Driver code public static void main(String[] args) { GFG circular_list = new GFG(); circular_list.add(10); circular_list.add(20); circular_list.add(30); circular_list.add(40); System.out.print("Original List --> "); circular_list.Print_List(); circular_list.Insert_In_Middle(60); System.out.print("List after Inserting --> "); circular_list.Print_List(); } } OutputOriginal List --> 10 20 30 40 List after Inserting --> 10 20 60 30 40 Time complexity: O(n) where n is no of nodes of circular linked list Auxiliary space: O(1) because using constant variables Comment More infoAdvertise with us Next Article C Program to Implement Circular Linked List U uchiha1101 Follow Improve Article Tags : Java Practice Tags : Java Similar Reads C Program to Implement Circular Linked List A linked list is a dynamic data structure where elements are not stored in contiguous memory locations but linked using pointers. In a circular linked list, the last node points back to the first node instead of pointing to NULL, forming a circle.In this article, we will learn how to implement the c 8 min read How to create a pointer to another pointer in a linked list? In this article we cover how to create a pointer to another pointer in a linked list, it can be singly, doubly, or circularly. What is Linked List: A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations. The elements in a linked list are linked 7 min read How to Add Element at First and Last Position of LinkedList in Java? LinkedList is a part of Collection framework present inside java.util package. This class is an implementation of LinkedList data structure which is a linear data structure where the elements are not stored in a contiguous manner and every element is a separate object with a data field and address f 2 min read LinkedList addFirst() Method in Java In Java, the addFirst() method of LinkedList, adds elements at the beginning of the list. All the existing elements moved one position to the right and the new element is placed at the beginning.Syntax of LinkedList addFirst() Method public void addFirst( E e)Parameters: E is the data type of elemen 1 min read Circular Linked List Implementation in Java A Circular Linked List is a variation of the traditional linked list data structure. In the traditional linked list, the last node points to the null and it can indicating the end of the list. However, in the circular linked list, the last node points back to the first node and forms the circle or l 7 min read Circular Linked List in C++ A circular linked list is a linear data structure similar to a singly linked list where each node consists of two data members data and a pointer next, that stores the address of the next node in the sequence but in a circular linked list, the last node of the list points to the first node instead o 10 min read Like