Delete Node from the End of Circular Linked List in Java



In this article, we will learn the remove the last node of the circular linked list in Java.

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.

Steps to delete a node from the ending of the circular linked list

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.

The following are the steps to delete a node from the ending of the circular linked list

  • Step 1 ? Define the listNode class: We define the listNode class to represent each node in the circular linked list. The class contains a value variable of type String and a next pointer to reference the next node. These variables are initialized in the constructor.
public class listNode {
String value;
listNode next;
public listNode(String val) {
this.value = val;
}
}
  • Step 2 ? Define the root and last pointers: We create two references, root and last, to track the first and last nodes of the circular linked list, respectively.
public listNode root = null;
public listNode last = null;

  • Step 3 ? Create the addNode() function: This function adds new nodes to the circular linked list. We initialize the new node with the provided value. If the list is empty, we make the new node both the root and last, setting its next pointer to the root. If the list is not empty, we update the next pointer of the current last node to point to the new node, then set the new node as the last and point it back to the root.
public void addNode(String val) {
	listNode newNode = new listNode(val);
	if (root == null) {
		
root = newNode; last = newNode;
newNode.next = root; } else { last.next = newNode; last = newNode; last.next = root; }
}

  • Step 4 ? Create the deleteLastNode() function: This function removes the last node from the circular linked list. If the list is empty, we return immediately. If the list has more than one node, we traverse the list to find the second-last node and update its next pointer to point to the root, making it the new last node. If there is only one node, we set both root and last to null, effectively deleting the node.

public void deleteLastNode() {
	if (root == null) {
		return;
	} else {
		if (root != last) {
			listNode temp = root;
			while (temp.next != last) {
				temp = temp.next;
			}
last = temp;
last.next = root; } else {
root = last = null; } } }

  • Step 5 ? Create the printList() function: The printList() function prints all the nodes in the circular linked list. If the list is empty, it prints a message indicating so. Otherwise, it traverses the list starting from the root, printing each node's value until it loops back to the root.

public void printList() {
	listNode temp = root;
	if (root == null) {
		System.out.println("The list is empty");
	} else {
		do {
			System.out.print(temp.value + " ");
			temp = temp.next;
		}
		while (temp != root);
		
System.out.println(); }
}

  • Step 6 ? Add nodes to the list: We use the addNode() method to add multiple nodes to the circular linked list. For example:
list.addNode("Hello");
list.addNode("World!");
list.addNode("How");
list.addNode("are");
list.addNode("You");
list.addNode("Doing?");

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.

Updated on: 2025-01-02T19:13:13+05:30

176 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements