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


In this DSA problem, we will learn the remove the last node of the circular linked list.

We set Null to the next pointer of the second-last node to remove the last node from the regular linked list, but in the circular linked list, we need to set the root node to the next pointer of the second-last node.

Problem Statement

We have given a circular linked list containing the N nodes. The given task is to delete the last node of the linked list.

Sample Examples

Input

Hello -> World! -> How -> are -> You -> Doing?

Output

Hello -> World! -> How -> are -> You

Explanation

We remove the last node.

Input

23 -> 4 -> 67 -> 89 -> 73 -> 100

Output

23 -> 4 -> 67 -> 89 -> 73

Explanation

We deleted the last node.

Approach 1

In this approach, we will find the second-last node of the circular linked list. While traversing the linked list, we can check whether the next pointer of the current node points to the last node. If yes, the current node is the second node, update its next pointer with the root node, and make the current node the last node.

Algorithm

  • Step 1 − Define the 'listNode' class for the node of the circular linked list. Also, define the 'value' variable of the string data type and the 'next' pointer of the listNode type in the class. Furthermore, initialize the variable values in the constructor.

  • Step 2 − Define the 'root' and 'last' pointers.

  • Step 3 − Create the addNode() function to add a node to the circular linked list.

  • Step 3.1 − In the addNode() function, initialize the new node with the given value.

  • Step 3.2 − If the root pointer is null, update the root and last node with null. Also, update the next pointer of the new node with the root pointer.

  • Step 3.3 − In other cases, connect the last node with the new node, make the new node to the last node, and connect the updated last node to the root node.

  • Step 4 − Create the deleteLastNode() function to remove the last node from the linked list.

  • Step 4.1 − For an empty list, execute the return statement.

  • Step 4.2 − When the root node and last node are not equal, traverse the linked list until the next pointer of the current node points towards the last node.

  • Step 4.3 − After that, make the second-last node to the last node, and connect it with the root node.

  • Step 4.4 − When the root node and last node are equal, update the root and last node with the null.

  • Step 5 − Create the printList() function to print the elements of the linked list.

  • Step 5.1 − Traverse the list using the do-while loop until the' temp' node equals the 'root' node, and print the value of each node.

  • Step 6 − Execute the addNode() method multiple times to add multiple nodes to the circular linked list.

  • Step 7 − Print the initial list using the printList() method. Now, execute the deleteLastNode() function to delete the last node and printList() method to show the updated list.

Example

public class Main {

   // Node for creating the linked list
   public class listNode {
      String value;
      listNode next;
      public listNode(String val) {
         this.value = val;
      }
   }
   
   // Root and last node initialization
   public listNode root = null;
   public listNode last = null;
   
   // Method to add new Node
   public void addNode(String val) {
   
      // Cerate new listNode
      listNode newNode = new listNode(val);
      
      // For the first node
      if (root == null) {
         root = newNode;
         last = newNode;
         newNode.next = root;
      }
      
      // Add new node after the last node. New node points to the root node
      else {
         last.next = newNode;
         last = newNode;
         last.next = root;
      }
   }
   public void deleteLastNode() {
   
      // For an empty list
      if (root == null) {
         return;
      }
      
      // Root node points to the next node. The last node points to the updated root node
      else {
         if (root != last) {
            listNode temp = root;
            while (temp.next != last) {
               temp = temp.next;
            }
            
            // Second last element is the new tail
            last = temp;
            last.next = root;
         }
         
         // For a list having a single node
         else {
            root = last = null;
         }
      }
   }
   
   // displaying the nodes
   public void printList() {
      listNode temp = root;
      if (root == null) {
         System.out.println("The list is empty");
      } else {
      
         // Traverse the list to show each node's value
         do {
            System.out.print(temp.value + " ");
            temp = temp.next;
         } while (temp != root);
            System.out.println();
      }
   }
   public static void main(String[] args) {
      Main list = new Main();
      
      // Adds data to the list
      list.addNode("Hello");
      list.addNode("World!");
      list.addNode("How");
      list.addNode("are");
      list.addNode("You");
      list.addNode("Doing?");
      
      // Printing the original list
      System.out.println("The initial list is :- ");
      list.printList();
      
      // Delete the first node
      list.deleteLastNode();
      System.out.println("After deleting the first node, the list is :- ");
      list.printList();
      
      // Delete the second node
      list.deleteLastNode();
      System.out.println("After deleting the second node, the list is :- ");
      list.printList();
   }
}

Output

The initial list is :- 
Hello World! How are You Doing? 
After deleting the first node, the list is :- 
Hello World! How are You 
After deleting the second node, the list is :- 
Hello World! How are
  • Time complexity − O(N), as we need to reach the last node.

  • Space complexity − O(1), as we don't use any extra space.

The logical part of removing the last node from the circular linked list is to find the second-last node and connect it with the root node. Programmers may try to delete the first node from the circular linked list for more practice.

Updated on: 24-Jul-2023

63 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements