Java Program to Delete a Node From the Middle of the Circular Linked List
In this article, we are going to learn to delete the middle node from the circular Linked List in java. The approach we are going to follow for this program is, first we calculate the number of nodes in the list and then divide the number of nodes by 2 to get the middle node of the list.
Case 1: List is empty
- Then simply just return from the function.
Case 2: List has only one node
- It means head == tail then assigned head and tail equal to null.
Case 3: List has only two nodes
- It means the middle node of the list at 1 position. In this case, we have to modify the head and tail also. Head becomes tail and tail.next becomes also tail.
Case 4: List has more than two nodes
- It means the position of the middle node is greater than 1. In this case, we define the two-node temp and prev. Assign head to the temp.
- Then traverse the list using temp up to the middle node. In each iteration assign prev = temp and temp = temp.next
- Now temp is pointing to the middle node and prev pointing to the previous node of the middle node.
- Then simply set prev.next=temp.next and temp=null,
List=10 20 30 40 List after deleting middle node=10 30 40 List after deleting middle node=10 40 List after deleting middle node=40 List after deleting middle node=List is empty
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. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.