Java Program to Insert a New Node at the Middle of the Circular Linked List
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:
- Create 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:
Original 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