Java Program to Delete a Node From the Middle of the Circular Linked List


In this DSA problem, we will learn to delete the middle node from the circular linked list.

We can delete the middle node from the circular linked list as we delete the middle node from the regular linked list. We need to locate the previous and next node of the middle node and connect them directly to remove the middle node.

Problem Statement

We have a circular linked list and need to delete the middle node from the linked list. If the linked list contains an even number of nodes, (N/2)th node is the middle node.

Sample Examples

Input

90 32 67 84 23 78

Output

90 32 84 23 78

Explanation

The 67 is deleted as it is a middle node.

Input

90 32 84 23 78

Output

90 32 23 78

Explanation

The 84 is deleted, as it is a middle node.

Approach 1

The first task is to count the total number of nodes in the linked list. If the total node is even, we delete the (N/2)th node. Otherwise, we can get the middle node if the total number of odds is even.

We need to locate the previous node of the middle node and update its next pointers value with the middle node's next pointers value to remove the middle node.

Algorithm

  • Step 1 − Define the 'dataNode' class containing the 'val' variable of integer type and the next pointer of the 'dataNode' type.

  • Step 2 − Define the static variable named 'listSize' to store the size of the linked list. Also, define the 'root' and 'end' pointer to store the first and last node.

  • Step 3 − Next, define the constructor for the class to initialize the 'root' and 'end' nodes and the size of the list with 0.

  • Step 4 - Define the addToList() function to add nodes to the circular linked list.

  • Step 4.1 − In the addToList() function, define the 'newNode' of type dataNode with the given value.

  • Step 4.2 − If the root node is null, update the root and end node with the newNode. Also, update the next pointer of the newNode with the root node.

  • Step 4.3 − Otherwise, point the next pointer of the last node to the newNode. Also, update the 'end' pointer value, and update the next pointer of the last node with the 'root' node.

  • Step 4.4 − Increment the 'listSize' by 1 after adding the node.

  • Step 5 − Define the middleDeletion() function to delete the middle node.

  • Step 5.1 − If the 'listSize' is even, initialize the mid with the 'listSize/2'. Otherwise, initialize the mid with the (listSize/2) + 1.

  • Step 5.2 − If the root is null, execute the return.

  • Step 5.3 − If the root and end are equal, update the root and end pointers with the null.

  • Step 5.4 − If mid is equal to 1, update the root node with the end node, and update the next pointer of the end node with the end.

  • Step 5.5 − In other cases, initialize the 'curr' with the root node, 'previous' with the null, and p with the 1.

  • Step 5.6 − Use the loop to make iteration until p is less than mid. In the loop, update the previous with the 'curr', 'curr' with its next, and increment the value of p by 1.

  • Step 5.7 − After that, update the next pointer of the 'previous' node with the next pointer of the 'curr' node. Also, set 'curr' to null and decrement the listSize by 1.

  • Step 6 − Define the showList() function to print the list.

  • Step 6.1 − In the function, traverse the list using the do-while loop, and print the value of each node.

  • Step 7 − Now, use the addToList() method to add nodes to the linked list.

  • Step 8 − Use the middleDeletion() function to delete the middle element and showList() to print the elements of the updated list.

Example

public class Main {
   class dataNode {
      int val;
      dataNode next;
   }
   private static int listSize;
   private dataNode root, end;
   
   // Class constructor
   Main() {
      this.root = null;
      this.end = null;
      listSize = 0;
   }
   public void addToList(int val) {
   
      // New node creation
      dataNode newNode = new dataNode();
      
      // For empty list
      if (this.root == null) {
         newNode.val = val;
         this.root = newNode;
         this.end = newNode;
         newNode.next = this.root;
      }
      
      // Append new node at last of the list. connect end node with the root node
      else {
         newNode.val = val;
         end.next = newNode;
         end = newNode;
         end.next = root;
      }
      listSize++;
   }
   public void middleDeletion() {
      int mid;
      dataNode curr, previous;
      
      // Middle position calculation
      if (listSize % 2 == 0) {
         mid = listSize / 2;
      } else {
         mid = (listSize / 2) + 1;
      }
      
      // For an empty list
      if (root == null) {
         return;
      }
      
      // For a list having a single node
      else if (root == end) {
         root = null;
         end = null;
      }
      
      // For list having two nodes
      else if (mid == 1) {
         root = end;
         end.next = end;
      }
      
      // For a list having three or more nodes
      else {
         curr = root;
         previous = null;
         int p = 1;
         
         // Reach to the middle node
         while (p < mid) {
            previous = curr;
            curr = curr.next;
            p++;
         }
         
         // Join the previous node with next node of middle
         previous.next = curr.next;
         curr = null;
      }
      listSize--;
      if (listSize < 0) {
         listSize = 0;
      }
   }
   public void showList() {
   
      // For an empty list
      if (root == null) {
         System.out.println("The list is empty");
      } else {
         dataNode curr = root;
         
         // Traverse the linked list
         do {
            System.out.print(curr.val + " ");
            curr = curr.next;
         } while (curr != root);
            System.out.println();
      }
   }
   public static void main(String args[]) {
      Main obj = new Main();
      
      // Add nodes
      obj.addToList(90);
      obj.addToList(32);
      obj.addToList(67);
      obj.addToList(84);
      obj.addToList(23);
      obj.addToList(78);
      System.out.print("Initial list is: ");
      obj.showList();
      
      // Middle node deletion
      obj.middleDeletion();
      System.out.print("The updated list after deleting the middle node is: ");
      obj.showList();
      
      // Middle node deletion
      obj.middleDeletion();
      System.out.print("The updated list after deleting the middle node is: ");
      obj.showList();
   }
}

Output

Initial list is: 90 32 67 84 23 78 
The updated list after deleting the middle node is: 90 32 84 23 78 
The updated list after deleting the middle node is: 90 32 23 78
  • Time complexity − O(N) to find the middle element.

  • Space complexity − O(1) as we use constant space.

The logical part is to find the previous and next node of the middle node to connect them to remove the middle node. Also, programmers may practice removing the starting or ending node from the circular linked list.

Updated on: 24-Jul-2023

95 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements