
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Delete Node from Beginning of Circular Linked List in Java
In this DSA problem, we will learn to create a circular linked list and delete the node from the beginning of that.
The circular linked list connects the last node with the first node. To remove the first node from the linked list, we can make the second node a root node, and connect the last node with the second node.
Problem statement
We have given a circular linked list. We need to delete the starting node of the linked list.
Sample examples
Input
1 -> 2 -> 3 -> 4 -> 8 -> 10
Output
2 -> 3 -> 4 -> 8 -> 10
Explanation
We have deleted the node from starting.
Input
100 -> 103 -> 200 -> 99 -> 56 -> 78 -> 532
Output
103 -> 200 -> 99 -> 56 -> 78 -> 532
Explanation
The first node from the circular linked list has been deleted.
Approach 1
In this approach, we will create the circular linked list first. While adding the node to the linked list, we will connect the last node with the root node. Furthermore, while deleting the first node, we will make the next node of the root node a root node and connect the last node with the updated root node.
Algorithm
Step 1 ? Create a 'listNode' class containing the 'value' of the integer data type and the' next' listNode pointer. Also, define the constructor to initialize the 'value' variable.
Step 2 ? Next, define the 'root' and 'last' pointers to store the first and last node of the linked list.
Step 3 ? Now, define the addValue() function to add the node to the linked list.
Step 3.1 ? Create a new listNode with the value we got as a parameter.
Step 3.2 ? If the root node is NULL, assign a new node to the 'root' and 'last' node. Also, connect the new node with the root node.
Step 3.3 ? If the root node is not NULL, connect the new node with the last node, update the value of the 'last' pointer, and connect the last node with the root node.
Step 4 ? Define the deleteFirstNode() function to delete the node from the start.
Step 4.1 ? In the deleteFirstNode() function, If the root is null, execute the return statement.
Step 4.2 ? If the root and last node are same, update the root and last with NULL.
Step 4.3 ? If the root and last node are not the same, update the root node with its next node, and connect the last node with the updated root node.
Step 5 ? Define the showList() function to print the list values.
Step 5.1 ? Initialize the 'temp' node with the root node.
Step 5.2 ? If the root is NULL, print the message that 'List is empty'.
Step 5.3 ? Otherwise, traverse the list, and print the value of the 'temp' node. Also, update the 'temp' node with its next node.
Step 6 ? In the main() method, create the object of the Main class.
Step 7 ? By taking the object of Main class as a reference, execute the addValue() method to add nodes to the linked list. After that, call the showList() to show the original list.
Step 8 ? Next, execute the deleteFirstNode() function to delete the first node, and call the showList() method to show the updated linked list.
Example
public class Main { // Node for creating the linked list public class listNode { int value; listNode next; // Constructor public listNode(int val) { this.value = val; } } // Root and last node initialization public listNode root = null; public listNode last = null; // Method to add new Node public void addValue(int 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 deleteFirstNode() { // 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) { root = root.next; last.next = root; } // For a list having a single node else { root = last = null; } } } // displaying the nodes public void showList() { 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.addValue(1); list.addValue(2); list.addValue(3); list.addValue(4); list.addValue(8); list.addValue(10); // Printing the original list System.out.println("The initial list is: - "); list.showList(); // Delete the first node list.deleteFirstNode(); System.out.println("After deleting the first node, the list is: - "); list.showList(); // Delete the second node list.deleteFirstNode(); System.out.println("After deleting the second node, the list is: - "); list.showList(); } }
Output
The initial list is: - 1 2 3 4 8 10 After deleting the first node, the list is: - 2 3 4 8 10 After deleting the second node, the list is: - 3 4 8 10
Time complexity ? O(1), as we modify the pointer in the constant time.
Space complexity ? O(1) as we don't use dynamic space.
We learned to delete the first node from the circular linked list. The programmer may learn to delete the last node of the circular linked list. In this case, programmers need to connect the last-second element of the linked list with the root node.