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 - O(1) Time and O(1) Space
To delete the first node of a circular linked list, check if the list is empty and return NULL if so. If it has only one node, delete it and set last = NULL. Otherwise, update last->next to skip the head, delete the head node, and return the updated last pointer.
Step-by-step approach:
- Check if List is Empty: If last is nullptr, print "List is empty" and return nullptr.
- Get Head Node: Set head to last->next.
- 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
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
Node(int value) {
data = value;
next = nullptr;
}
};
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 << " <-> ";
}
cout << endl;
}
int main() {
Node* first = new Node(2);
first->next = new Node(3);
first->next->next = new Node(4);
Node* last = first->next->next;
last->next = first;
last = deleteFirstNode(last);
printList(last);
return 0;
}
#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(" <-> ");
}
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;
last = deleteFirstNode(last);
printList(last);
return 0;
}
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.print(" <-> ");
}
System.out.println();
}
public static void main(String[] args) {
Node first = new Node(2);
first.next = new Node(3);
first.next.next = new Node(4);
Node last = first.next.next;
last.next = first;
last = deleteFirstNode(last);
printList(last);
}
}
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 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(" <-> ",end="")
print()
if __name__ == "__main__":
first = Node(2)
first.next = Node(3)
first.next.next = Node(4)
last = first.next.next
last.next = first
last = deleteFirstNode(last)
printList(last)
using System;
public class Node {
public int data;
public Node next;
public Node(int value) {
data = value;
next = null;
}
}
public class GfG {
public static Node deleteFirstNode(Node last) {
if (last == null) {
return null;
}
Node head = last.next;
if (head == last) {
head = null;
last = null;
} else {
last.next = head.next;
head = null;
}
return last;
}
public static void printList(Node last) {
if (last == null) return;
Node head = last.next;
while (true) {
Console.Write(head.data);
head = head.next;
if (head == last.next) break;
Console.Write(" <-> ");
}
Console.WriteLine();
}
public static void Main() {
Node first = new Node(2);
first.next = new Node(3);
first.next.next = new Node(4);
Node last = first.next.next;
last.next = first;
last = deleteFirstNode(last);
printList(last);
}
}
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 result = '';
let head = last.next;
while (true) {
result+=head.data;
head = head.next;
if (head === last.next) break;
result+=" <-> ";
}
console.log(result.trim());
}
// Driver Code
let first = new Node(2);
first.next = new Node(3);
first.next.next = new Node(4);
let last = first.next.next;
last.next = first;
last = deleteFirstNode(last);
printList(last);
Output
3 <-> 4
2. Deletion at specific position in circular linked list - O(n) Time and O(1) Space
To delete a specific node from a circular linked list, check if the list is empty. If it has only one node and matches the key, delete it and set the list to empty. Otherwise, traverse with two pointers to find the node. If found, adjust the links to remove it and return the updated list; if not found, leave the list unchanged.
Step-by-step approach:
- Check if the list is empty (last is None). If yes, return None.
- Set curr to last.next (head) and prev to last.
- If the list has only one node (curr == last) and the node’s data matches key, delete it by setting last = None and return.
- If the head node’s data matches key, update last.next to skip the head node and return last.
- Otherwise, traverse the list using curr and prev until you either reach back to last or find the node with key.
- If the node with key is found:
=> Update prev.next to skip curr
=> If curr is the last node, update last = prev. - Return the updated last.
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
Node(int value) {
data = value;
next = nullptr;
}
};
Node* deleteSpecificNode(Node* last, int key) {
if (last == nullptr) {
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;
}
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 << " <-> ";
}
cout << endl;
}
int main() {
Node* first = new Node(2);
first->next = new Node(3);
first->next->next = new Node(4);
Node* last = first->next->next;
last->next = first;
int key = 3;
last = deleteSpecificNode(last, key);
printList(last);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* deleteSpecificNode(struct Node* last, int key) {
if (last == NULL) {
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);
}
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(" <-> ");
}
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;
int key = 3;
last = deleteSpecificNode(last, key);
printList(last);
return 0;
}
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) {
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;
}
}
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.print(" <-> ");
}
System.out.println();
}
public static void main(String[] args){
Node first = new Node(2);
first.next = new Node(3);
first.next.next = new Node(4);
Node last = first.next.next;
last.next = first;
int key = 3;
last = deleteSpecificNode(last, key);
printList(last);
}
}
class Node:
def __init__(self, data):
self.data = data
self.next = None
def deleteSpecificNode(last, key):
if last is None:
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
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(" <-> ",end="")
print()
if __name__ == "__main__":
first = Node(2)
first.next = Node(3)
first.next.next = Node(4)
last = first.next.next
last.next = first
key = 3
last = deleteSpecificNode(last, key)
printList(last)
using System;
public class Node
{
public int Data { get; set; }
public Node Next { get; set; }
}
public class GfG {
public static Node deleteSpecificNode(Node last, int key) {
if (last == null)
return null;
Node curr = last.Next;
Node prev = last;
// If the node to be deleted is the only node
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 to find the node
while (curr != last && curr.Data != key) {
prev = curr;
curr = curr.Next;
}
// If node found
if (curr.Data == key) {
prev.Next = curr.Next;
// If deleting the last node
if (curr == last)
last = prev;
}
return last;
}
public static void printList(Node last) {
if (last == null) {
return;
}
Node head = last.Next;
while (true) {
Console.Write(head.Data);
head = head.Next;
if (head == last.Next) break;
Console.Write(" <-> ");
}
Console.WriteLine();
}
public static Node createNode(int value) {
return new Node { Data = value, Next = null };
}
public static void Main(string[] args) {
Node first = createNode(2);
first.Next = createNode(3);
first.Next.Next = createNode(4);
Node last = first.Next.Next;
last.Next = first;
int key = 3;
last = deleteSpecificNode(last, key);
printList(last);
}
}
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
function deleteSpecificNode(last, key) {
if (last === null) {
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;
}
}
return last;
}
function printList(last) {
if (last === null) return;
let result = '';
let head = last.next;
while (true) {
result+=head.data;
head = head.next;
if (head === last.next) break;
result+=" <-> ";
}
console.log(result.trim());
}
// Driver Code
let first = new Node(2);
first.next = new Node(3);
first.next.next = new Node(4);
let last = first.next.next;
last.next = first;
let key = 3;
last = deleteSpecificNode(last, key);
printList(last);
Output
2 <-> 4
3. Deletion at the end of Circular linked list - O(n) Time and O(1) Space
To delete the last node in a circular linked list, first check if the list is empty. If it has only one node, delete it and set last to nullptr. Otherwise, traverse to the second last node, update its next to point to the head, delete the last node, and update last to the second last node.
Step-by-step approach:
- Check if the list is empty (last == None). If yes, return None.
- Set head = last.next.
- If the list has only one node (head == last), delete it by setting last = None and return.
- Traverse the list using a pointer curr starting from head until curr.next == last.
- Update curr.next = head to skip the last node.
- Delete the last node and update last = curr.
- Return the updated last.
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
Node(int value) {
data = value;
next = nullptr;
}
};
Node* deleteLastNode(Node* last) {
if (last == nullptr) {
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 << " <-> ";
}
cout << endl;
}
int main() {
Node* first = new Node(2);
first->next = new Node(3);
first->next->next = new Node(4);
Node* last = first->next->next;
last->next = first;
last = deleteLastNode(last);
printList(last);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* deleteLastNode(struct Node* last) {
if (last == NULL) {
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(" <-> ");
}
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;
last = deleteLastNode(last);
printList(last);
return 0;
}
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) {
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.print(" <-> ");
}
System.out.println();
}
public static void main(String[] args){
Node first = new Node(2);
first.next = new Node(3);
first.next.next = new Node(4);
Node last = first.next.next;
last.next = first;
last = deleteLastNode(last);
printList(last);
}
}
class Node:
def __init__(self, data):
self.data = data
self.next = None
def deleteLastNode(last):
if last is None:
return None
head = last.next
# If there is only one node in the list
if head == last:
last = None
return last
# Traverse to find the second last node
curr = head
while curr.next != last:
curr = curr.next
# Update pointers
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(" <-> ",end="")
print()
if __name__ == "__main__":
first = Node(2)
first.next = Node(3)
first.next.next = Node(4)
last = first.next.next
last.next = first
last = deleteLastNode(last)
printList(last)
using System;
public class Node {
public int data;
public Node next;
public Node(int value) {
data = value;
next = null;
}
}
public class GfG {
public Node deleteLastNode(Node last) {
if (last == null) {
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 void printList(Node last) {
if (last == null) return;
Node head = last.next;
while (true) {
Console.Write(head.data);
head = head.next;
if (head == last.next) break;
Console.Write(" <-> ");
}
Console.WriteLine();
}
public static void Main() {
Node first = new Node(2);
first.next = new Node(3);
first.next.next = new Node(4);
Node last = first.next.next;
last.next = first;
GfG list = new GfG();
last = list.deleteLastNode(last);
list.printList(last);
}
}
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
function deleteLastNode(last) {
if (last === null) {
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 result = '';
let head = last.next;
while (true) {
result+=head.data;
head = head.next;
if (head === last.next) break;
result+=" <-> ";
}
console.log(result.trim());
}
// Driver Code
let first = new Node(2);
first.next = new Node(3);
first.next.next = new Node(4);
let last = first.next.next;
last.next = first;
last = deleteLastNode(last);
printList(last);
Output
2 <-> 3