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
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.