Deletion from a Circular Linked List
Last Updated : 19 Dec, 2024
In this article, we will learn how to delete a node from a circular linked list. In a circular linked list, the last node connects back to the first node, creating a loop.
There are three main ways to delete a node from circular linked list:
- Deletion at the beginning
- Deletion at specific position
- Deletion at the end
Now, let’s look at the methods and steps for these three deletion operations.
Deletion from a Circular Linked List:
Deletion involves removing a node from the linked list. The main difference is that we need to ensure the list remains circular after the deletion. We can delete a node in a circular linked list in three ways:
1. Deletion from the beginning of the circular linked list
To delete the first node of a circular linked list, we first check if the list is empty. If it is then we print a message and return NULL. If the list contains only one node (the head is the same as the last) then we delete that node and set the last pointer to NULL. If there are multiple nodes then we update the last->next pointer to skip the head node and effectively removing it from the list. We then delete the head node to free the allocated memory. Finally, we return the updated last pointer, which still points to the last node in the list.
Delete the first node in circular linked listStep-by-step approach:
- Check if List is Empty:
- If last is nullptr, print "List is empty" and return nullptr.
- Get Head Node:
- Check for Single Node:
- If head equals last, delete head and set last to nullptr.
- Handle Multiple Nodes:
- Update last->next to point to head->next.
- Delete head.
- Return Updated last
Below is the implementation of the above approach:
C++ #include <iostream> using namespace std; struct Node { int data; Node* next; Node(int value) { data = value; next = nullptr; } }; // Function to delete the first node of the circular linked list Node* deleteFirstNode(Node* last) { if (last == nullptr) { // If the list is empty cout << "List is empty" << endl; return nullptr; } Node* head = last->next; if (head == last) { // If there is only one node in the list delete head; last = nullptr; } else { // More than one node in the list last->next = head->next; delete head; } return last; } void printList(Node* last) { if(last == NULL) return ; Node *head = last->next; while (true){ cout << head->data << " "; head = head->next; if (head == last->next) break; } cout << endl; } int main() { // Create circular linked list: 2, 3, 4 Node* first = new Node(2); first->next = new Node(3); first->next->next = new Node(4); Node* last = first->next->next; last->next = first; cout << "Original list: "; printList(last); // Delete the first node last = deleteFirstNode(last); cout << "List after deleting first node: "; printList(last); return 0; }
C #include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* next; }; struct Node* deleteFirstNode(struct Node* last) { if (last == NULL) { // If the list is empty printf("List is empty\n"); return NULL; } struct Node* head = last->next; if (head == last) { // If there is only one node in the list free(head); last = NULL; } else { // More than one node in the list last->next = head->next; free(head); } return last; } void printList(struct Node* last) { if (last == NULL) return; struct Node* head = last->next; while (1) { printf("%d ", head->data); head = head->next; if (head == last->next) break; } printf("\n"); } struct Node* createNode(int value) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = value; newNode->next = NULL; return newNode; } int main() { struct Node* first = createNode(2); first->next = createNode(3); first->next->next = createNode(4); struct Node* last = first->next->next; last->next = first; printf("Original list: "); printList(last); last = deleteFirstNode(last); printf("List after deleting first node: "); printList(last); return 0; }
Java class Node { int data; Node next; Node(int value) { data = value; next = null; } } public class GFG { public static Node deleteFirstNode(Node last) { if (last == null) { // If the list is empty System.out.println("List is empty"); return null; } Node head = last.next; if (head == last) { // If there is only one node in the list last = null; } else { // More than one node in the list last.next = head.next; } return last; } public static void printList(Node last) { if (last == null) return; Node head = last.next; while (true) { System.out.print(head.data + " "); head = head.next; if (head == last.next) break; } System.out.println(); } public static void main(String[] args) { // Create circular linked list: 2, 3, 4 Node first = new Node(2); first.next = new Node(3); first.next.next = new Node(4); Node last = first.next.next; last.next = first; System.out.print("Original list: "); printList(last); // Delete the first node last = deleteFirstNode(last); System.out.print("List after deleting first node: "); printList(last); } }
Python class Node: def __init__(self, data): self.data = data self.next = None def deleteFirstNode(last): if last is None: # If the list is empty print("List is empty") return None head = last.next if head == last: # If there is only one node in the list last = None else: # More than one node in the list last.next = head.next return last def print_list(last): if last is None: return head = last.next while True: print(head.data, end=" ") head = head.next if head == last.next: break print() # Create circular linked list: 2, 3, 4 first = Node(2) first.next = Node(3) first.next.next = Node(4) last = first.next.next last.next = first print("Original list: ", end="") print_list(last) # Delete the first node last = deleteFirstNode(last) print("List after deleting first node: ", end="") print_list(last)
JavaScript class Node { constructor(data) { this.data = data; this.next = null; } } function deleteFirstNode(last) { if (last === null) { // If the list is empty console.log("List is empty"); return null; } let head = last.next; if (head === last) { // If there is only one node in the list last = null; } else { // More than one node in the list last.next = head.next; } return last; } function printList(last) { if (last === null) return; let head = last.next; while (true) { console.log(head.data + " "); head = head.next; if (head === last.next) break; } console.log(); } // Create circular linked list: 2, 3, 4 let first = new Node(2); first.next = new Node(3); first.next.next = new Node(4); let last = first.next.next; last.next = first; console.log("Original list: "); printList(last); // Delete the first node last = deleteFirstNode(last); console.log("List after deleting first node: "); printList(last);
OutputOriginal list: 2 3 4 List after deleting first node: 3 4
Time Complexity: O(1)
Auxiliary Space: O(1)
2. Deletion at specific position in circular linked list
To delete a specific node from a circular linked list, we first check if the list is empty. If it is then we print a message and return nullptr. If the list contains only one node and it matches the key then we delete that node and set last to nullptr. If the node to be deleted is the first node then we update the next pointer of the last node to skip the head node and delete the head. For other nodes, we traverse the list using two pointers: curr (to find the node) and prev (to keep track of the previous node). If we find the node with the matching key then we update the next pointer of prev to skip the curr node and delete it. If the node is found and it is the last node, we update the last pointer accordingly. If the node is not found then do nothing and tail or last as it is. Finally, we return the updated last pointer.
Delete a specific node in circular linked listStep-by-step approach:
- Check if List is Empty (last is nullptr) then print "List is empty, nothing to delete" and return nullptr.
- Set curr to last->next (head) and prev to last.
- Check for single node, if node's data matches key, delete it and set last to nullptr.
- Check for first node to delete, if head node's data matches key, update last->next and delete the head.
- Loop through the list to find the node with the specified key.
- If node found then update prev->next to skip the deleted node. If the deleted node is last, update last to prev.
- Otherwise, print "Node with data [key] not found."
- Return the modified last.
Below is the implementation of the above approach:
C++ #include <iostream> using namespace std; struct Node { int data; Node* next; Node(int value) { data = value; next = nullptr; } }; // Function to delete a specific node in the circular linked list Node* deleteSpecificNode(Node* last, int key) { if (last == nullptr) { // If the list is empty cout << "List is empty, nothing to delete." << endl; return nullptr; } Node* curr = last->next; Node* prev = last; // If the node to be deleted is the only node in the list if (curr == last && curr->data == key) { delete curr; last = nullptr; return last; } // If the node to be deleted is the first node if (curr->data == key) { last->next = curr->next; delete curr; return last; } // Traverse the list to find the node to be deleted while (curr != last && curr->data != key) { prev = curr; curr = curr->next; } // If the node to be deleted is found if (curr->data == key) { prev->next = curr->next; if (curr == last) { last = prev; } delete curr; } else { // If the node to be deleted is not found cout << "Node with data " << key << " not found." << endl; } return last; } // Function to print the circular linked list void printList(Node* last) { if (last == NULL){ cout << "List is Empty"; return; } Node *head = last->next; while (true){ cout << head->data << " "; head = head->next; if (head == last->next) break; } cout << endl; } int main() { // Create circular linked list: 2, 3, 4 Node* first = new Node(2); first->next = new Node(3); first->next->next = new Node(4); Node* last = first->next->next; last->next = first; cout << "Original list: "; printList(last); // Delete a specific node int key = 3; last = deleteSpecificNode(last, key); cout << "List after deleting node " << key << ": "; printList(last); return 0; }
C #include <stdio.h> #include <stdlib.h> // Define the structure for a node in the circular linked list struct Node { int data; struct Node* next; }; // Function to delete a specific node in the circular linked list struct Node* deleteSpecificNode(struct Node* last, int key) { if (last == NULL) { // If the list is empty printf("List is empty, nothing to delete.\n"); return NULL; } struct Node* curr = last->next; struct Node* prev = last; // If the node to be deleted is the only node in the list if (curr == last && curr->data == key) { free(curr); last = NULL; return last; } // If the node to be deleted is the first node if (curr->data == key) { last->next = curr->next; free(curr); return last; } // Traverse the list to find the node to be deleted while (curr != last && curr->data != key) { prev = curr; curr = curr->next; } // If the node to be deleted is found if (curr->data == key) { prev->next = curr->next; if (curr == last) { last = prev; } free(curr); } else { // If the node to be deleted is not found printf("Node with data %d not found.\n", key); } return last; } void printList(struct Node* last) { if (last == NULL) { printf("List is Empty"); return; } struct Node* head = last->next; while (1) { printf("%d ", head->data); head = head->next; if (head == last->next) break; } printf("\n"); } struct Node* createNode(int value) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = value; newNode->next = NULL; return newNode; } int main() { // Create circular linked list: 2, 3, 4 struct Node* first = createNode(2); first->next = createNode(3); first->next->next = createNode(4); struct Node* last = first->next->next; last->next = first; printf("Original list: "); printList(last); // Delete a specific node int key = 3; last = deleteSpecificNode(last, key); printf("List after deleting node %d: ", key); printList(last); return 0; }
Java class Node { int data; Node next; Node(int value){ data = value; next = null; } } public class GFG { public static Node deleteSpecificNode(Node last, int key){ if (last == null) { // If the list is empty System.out.println( "List is empty, nothing to delete."); return null; } Node curr = last.next; Node prev = last; // If the node to be deleted is the only node in the // list if (curr == last && curr.data == key) { last = null; return last; } // If the node to be deleted is the first node if (curr.data == key) { last.next = curr.next; return last; } // Traverse the list to find the node to be deleted while (curr != last && curr.data != key) { prev = curr; curr = curr.next; } // If the node to be deleted is found if (curr.data == key) { prev.next = curr.next; if (curr == last) { last = prev; } } else { // If the node to be deleted is not found System.out.println("Node with data " + key + " not found."); } return last; } public static void printList(Node last){ if (last == null) { System.out.println("List is Empty"); return; } Node head = last.next; while (true) { System.out.print(head.data + " "); head = head.next; if (head == last.next) break; } System.out.println(); } public static void main(String[] args){ // Create circular linked list: 2, 3, 4 Node first = new Node(2); first.next = new Node(3); first.next.next = new Node(4); Node last = first.next.next; last.next = first; System.out.print("Original list: "); printList(last); // Delete a specific node int key = 3; last = deleteSpecificNode(last, key); System.out.print("List after deleting node " + key + ": "); printList(last); } }
Python class Node: def __init__(self, data): self.data = data self.next = None def deleteSpecificNode(last, key): if last is None: # If the list is empty print("List is empty, nothing to delete.") return None curr = last.next prev = last # If the node to be deleted is the only node in the list if curr == last and curr.data == key: last = None return last # If the node to be deleted is the first node if curr.data == key: last.next = curr.next return last # Traverse the list to find the node to be deleted while curr != last and curr.data != key: prev = curr curr = curr.next # If the node to be deleted is found if curr.data == key: prev.next = curr.next if curr == last: last = prev else: # If the node to be deleted is not found print(f"Node with data {key} not found.") return last def printList(last): if last is None: print("List is Empty") return head = last.next while True: print(head.data, end=" ") head = head.next if head == last.next: break print() # Create circular linked list: 2, 3, 4 first = Node(2) first.next = Node(3) first.next.next = Node(4) last = first.next.next last.next = first print("Original list: ", end="") printList(last) # Delete a specific node key = 3 last = deleteSpecificNode(last, key) print(f"List after deleting node {key}: ", end="") printList(last)
JavaScript class Node { constructor(data) { this.data = data; this.next = null; } } function deleteSpecificNode(last, key) { if (last === null) { // If the list is empty console.log("List is empty, nothing to delete."); return null; } let curr = last.next; let prev = last; // If the node to be deleted is the only node in the list if (curr === last && curr.data === key) { last = null; return last; } // If the node to be deleted is the first node if (curr.data === key) { last.next = curr.next; return last; } // Traverse the list to find the node to be deleted while (curr !== last && curr.data !== key) { prev = curr; curr = curr.next; } // If the node to be deleted is found if (curr.data === key) { prev.next = curr.next; if (curr === last) { last = prev; } } else { // If the node to be deleted is not found console.log("Node with data " + key + " not found."); } return last; } function printList(last) { if (last === null) { console.log("List is Empty"); return; } let head = last.next; while (true) { console.log(head.data + " "); head = head.next; if (head === last.next) break; } console.log(); } // Create circular linked list: 2, 3, 4 let first = new Node(2); first.next = new Node(3); first.next.next = new Node(4); let last = first.next.next; last.next = first; console.log("Original list: "); printList(last); // Delete a specific node let key = 3; last = deleteSpecificNode(last, key); console.log("List after deleting node " + key + ": "); printList(last);
OutputOriginal list: 2 3 4 List after deleting node 3: 2 4
Time Complexity: O(n), due to traversing the list to find the specific position
Auxiliary Space: O(1)
3. Deletion at the end of Circular linked list
To delete the last node in a circular linked list, we first check if the list is empty. If it is, we print a message and return nullptr. If the list contains only one node (where the head is the same as the last), we delete that node and set last to nullptr. For lists with multiple nodes, we need to traverse the list to find the second last node. We do this by starting from the head and moving through the list until we reach the node whose next pointer points to last. Once we find the second last node then we update its next pointer to point back to the head, this effectively removing the last node from the list. We then delete the last node to free up memory and return the updated last pointer, which now points to the last node.
Deletion at the end of Circular linked listStep-by-step approach:
- Check if List is Empty (last is nullptr) then print "List is empty, nothing to delete" and return nullptr.
- Get head node by Setting head to last->next.
- Check for single node, if head equals last, delete last and set last to nullptr.
- Find second last node by traversing the list until curr->next equals last.
- Update Pointer by setting curr->next to point to head.
- Delete last and update last to curr.
- Return the updated last.
Below is the implementation of the above approach:
C++ #include <iostream> using namespace std; struct Node { int data; Node* next; Node(int value) { data = value; next = nullptr; } }; // Function to delete the last node in the circular linked list Node* deleteLastNode(Node* last) { if (last == nullptr) { // If the list is empty cout << "List is empty, nothing to delete." << endl; return nullptr; } Node* head = last->next; // If there is only one node in the list if (head == last) { delete last; last = nullptr; return last; } // Traverse the list to find the second last node Node* curr = head; while (curr->next != last) { curr = curr->next; } // Update the second last node's next pointer // to point to head curr->next = head; delete last; last = curr; return last; } void printList(Node* last) { if(last == NULL) return; Node *head = last->next; while (true){ cout << head->data << " "; head = head->next; if (head == last->next) break; } cout << endl; } int main() { // Create circular linked list: 2, 3, 4 Node* first = new Node(2); first->next = new Node(3); first->next->next = new Node(4); Node* last = first->next->next; last->next = first; cout << "Original list: "; printList(last); // Delete the last node last = deleteLastNode(last); cout << "List after deleting last node: "; printList(last); return 0; }
C #include <stdio.h> #include <stdlib.h> // Define the structure for a node in the circular linked list struct Node { int data; struct Node* next; }; // Function to delete the last node in the circular linked list struct Node* deleteLastNode(struct Node* last) { if (last == NULL) { // If the list is empty printf("List is empty, nothing to delete.\n"); return NULL; } struct Node* head = last->next; // If there is only one node in the list if (head == last) { free(last); last = NULL; return last; } // Traverse the list to find the second last node struct Node* curr = head; while (curr->next != last) { curr = curr->next; } // Update the second last node's next pointer to point to head curr->next = head; free(last); last = curr; return last; } void printList(struct Node* last) { if (last == NULL) return; struct Node* head = last->next; while (1) { printf("%d ", head->data); head = head->next; if (head == last->next) break; } printf("\n"); } struct Node* createNode(int value) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = value; newNode->next = NULL; return newNode; } int main() { // Create circular linked list: 2, 3, 4 struct Node* first = createNode(2); first->next = createNode(3); first->next->next = createNode(4); struct Node* last = first->next->next; last->next = first; printf("Original list: "); printList(last); // Delete the last node last = deleteLastNode(last); printf("List after deleting last node: "); printList(last); return 0; }
Java class Node { int data; Node next; Node(int value){ data = value; next = null; } } public class GFG { public static Node deleteLastNode(Node last){ if (last == null) { // If the list is empty System.out.println( "List is empty, nothing to delete."); return null; } Node head = last.next; // If there is only one node in the list if (head == last) { last = null; return last; } // Traverse the list to find the second last node Node curr = head; while (curr.next != last) { curr = curr.next; } // Update the second last node's next pointer to // point to head curr.next = head; last = curr; return last; } public static void printList(Node last){ if (last == null) return; Node head = last.next; while (true) { System.out.print(head.data + " "); head = head.next; if (head == last.next) break; } System.out.println(); } public static void main(String[] args){ // Create circular linked list: 2, 3, 4 Node first = new Node(2); first.next = new Node(3); first.next.next = new Node(4); Node last = first.next.next; last.next = first; System.out.print("Original list: "); printList(last); // Delete the last node last = deleteLastNode(last); System.out.print("List after deleting last node: "); printList(last); } }
Python class Node: def __init__(self, data): self.data = data self.next = None def deleteLastNode(last): if last is None: # If the list is empty print("List is empty, nothing to delete.") return None head = last.next # If there is only one node in the list if head == last: last = None return last # Traverse the list to find the second last node curr = head while curr.next != last: curr = curr.next # Update the second last node's next pointer to point to head curr.next = head last = curr return last def printList(last): if last is None: return head = last.next while True: print(head.data, end=" ") head = head.next if head == last.next: break print() # Create circular linked list: 2, 3, 4 first = Node(2) first.next = Node(3) first.next.next = Node(4) last = first.next.next last.next = first print("Original list: ", end="") printList(last) # Delete the last node last = deleteLastNode(last) print("List after deleting last node: ", end="") printList(last)
C# using System; public class Node { public int data; public Node next; public Node(int value) { data = value; next = null; } } public class GFG { // Function to delete the last node in the circular // linked list public static Node deleteLastNode(Node last) { if (last == null) { // If the list is empty Console.WriteLine( "List is empty, nothing to delete."); return null; } Node head = last.next; // If there is only one node in the list if (head == last) { last = null; return last; } // Traverse the list to find the second last node Node curr = head; while (curr.next != last) { curr = curr.next; } // Update the second last node's next pointer // to point to head curr.next = head; last = curr; return last; } // Function to print the circular linked list public static void printList(Node last) { if (last == null) { Console.WriteLine("List is Empty"); return; } Node head = last.next; while (true) { Console.Write(head.data + " "); head = head.next; if (head == last.next) break; } Console.WriteLine(); } public static void Main(string[] args) { // Create circular linked list: 2, 3, 4 Node first = new Node(2); first.next = new Node(3); first.next.next = new Node(4); Node last = first.next.next; last.next = first; Console.Write("Original list: "); printList(last); // Delete the last node last = deleteLastNode(last); Console.Write("List after deleting last node: "); printList(last); } }
JavaScript class Node { constructor(data) { this.data = data; this.next = null; } } function deleteLastNode(last) { if (last === null) { // If the list is empty console.log("List is empty, nothing to delete."); return null; } let head = last.next; // If there is only one node in the list if (head === last) { last = null; return last; } // Traverse the list to find the second last node let curr = head; while (curr.next !== last) { curr = curr.next; } // Update the second last node's next pointer to point to head curr.next = head; last = curr; return last; } function printList(last) { if (last === null) return; let head = last.next; while (true) { process.stdout.write(head.data + " "); head = head.next; if (head === last.next) break; } console.log(); } // Create circular linked list: 2, 3, 4 let first = new Node(2); first.next = new Node(3); first.next.next = new Node(4); let last = first.next.next; last.next = first; console.log("Original list: "); printList(last); // Delete the last node last = deleteLastNode(last); console.log("List after deleting last node: "); printList(last);
OutputOriginal list: 2 3 4 List after deleting last node: 2 3
Time Complexity: O(N)
Auxiliary Space: O(1)