We have already discussed circular linked list and traversal in a circular linked list in the below articles:
Introduction to circular linked list
Traversal in a circular linked list
In this article, we will learn about deleting a node from a circular linked list. Consider the linked list as shown below:

We will be given a node and our task is to delete that node from the circular linked list.
Examples:
Input : 2->5->7->8->10->(head node)
data = 5
Output : 2->7->8->10->(head node)
Input : 2->5->7->8->10->(head node)
7
Output : 2->5->8->10->(head node)
Algorithm
Case 1: List is empty.
- If the list is empty we will simply return.
Case 2:List is not empty
- If the list is not empty then we define two pointers curr and prev and initialize the pointer curr with the head node.
- Traverse the list using curr to find the node to be deleted and before moving to curr to the next node, every time set prev = curr.
- If the node is found, check if it is the only node in the list. If yes, set head = NULL and free(curr).
- If the list has more than one node, check if it is the first node of the list. Condition to check this( curr == head). If yes, then move prev until it reaches the last node. After prev reaches the last node, set head = head -> next and prev -> next = head. Delete curr.
- If curr is not the first node, we check if it is the last node in the list. Condition to check this is (curr -> next == head).
- If curr is the last node. Set prev -> next = head and delete the node curr by free(curr).
- If the node to be deleted is neither the first node nor the last node, then set prev -> next = curr -> next and delete curr.
Complete program to demonstrate deletion in Circular Linked List:
C++14
#include <bits/stdc++.h>
using namespace std;
class Node {
public:
int data;
Node* next;
};
void push(Node** head_ref, int data)
{
Node* ptr1 = new Node();
ptr1->data = data;
ptr1->next = *head_ref;
if (*head_ref != NULL)
{
Node* temp = *head_ref;
while (temp->next != *head_ref)
temp = temp->next;
temp->next = ptr1;
}
else
ptr1->next = ptr1;
*head_ref = ptr1;
}
void printList(Node* head)
{
Node* temp = head;
if (head != NULL) {
do {
cout << temp->data << " ";
temp = temp->next;
} while (temp != head);
}
cout << endl;
}
void deleteNode(Node** head, int key)
{
if (*head == NULL)
return;
if((*head)->data==key && (*head)->next==*head)
{
free(*head);
*head=NULL;
return;
}
Node *last=*head,*d;
if((*head)->data==key)
{
while(last->next!=*head)
last=last->next;
last->next=(*head)->next;
free(*head);
*head=last->next;
return;
}
while(last->next!=*head&&last-;>next->data!=key)
{
last=last->next;
}
if(last->next->data==key)
{
d=last->next;
last->next=d->next;
free(d);
}
else
cout<<"no such keyfound";
}
int main()
{
Node* head = NULL;
push(&head;, 2);
push(&head;, 5);
push(&head;, 7);
push(&head;, 8);
push(&head;, 10);
cout << "List Before Deletion: ";
printList(head);
deleteNode(&head;, 7);
cout << "List After Deletion: ";
printList(head);
return 0;
}
|
C
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void push(struct Node** head_ref, int data)
{
struct Node* ptr1 = (struct Node*)malloc(sizeof(struct Node));
ptr1->data = data;
ptr1->next = *head_ref;
if (*head_ref != NULL) {
struct Node* temp = *head_ref;
while (temp->next != *head_ref)
temp = temp->next;
temp->next = ptr1;
}
else
ptr1->next = ptr1;
*head_ref = ptr1;
}
void printList(struct Node* head)
{
struct Node* temp = head;
if (head != NULL) {
do {
printf("%d ", temp->data);
temp = temp->next;
} while (temp != head);
}
printf("\n");
}
void deleteNode(struct Node* head, int key)
{
if (head == NULL)
return;
struct Node *curr = head, *prev;
while (curr->data != key)
{
if (curr->next == head)
{
printf("\nGiven node is not found"
" in the list!!!");
break;
}
prev = curr;
curr = curr->next;
}
if (curr->next == head)
{
head = NULL;
free(curr);
return;
}
if (curr == head)
{
prev = head;
while (prev->next != head)
prev = prev->next;
head = curr->next;
prev->next = head;
free(curr);
}
else if (curr->next == head && curr == head)
{
prev->next = head;
free(curr);
}
else
{
prev->next = curr->next;
free(curr);
}
}
int main()
{
struct Node* head = NULL;
push(&head;, 2);
push(&head;, 5);
push(&head;, 7);
push(&head;, 8);
push(&head;, 10);
printf("List Before Deletion: ");
printList(head);
deleteNode(head, 7);
printf("List After Deletion: ");
printList(head);
return 0;
}
|
Java
import java.util.*;
import java.io.*;
public class GFG {
static class Node {
int data;
Node next;
};
static Node push(Node head_ref, int data)
{
Node ptr1 = new Node();
ptr1.data = data;
ptr1.next = head_ref;
if (head_ref != null) {
Node temp = head_ref;
while (temp.next != head_ref)
temp = temp.next;
temp.next = ptr1;
}
else
ptr1.next = ptr1;
head_ref = ptr1;
return head_ref;
}
static void printList(Node head)
{
Node temp = head;
if (head != null) {
do {
System.out.printf("%d ", temp.data);
temp = temp.next;
} while (temp != head);
}
System.out.printf("\n");
}
static Node deleteNode(Node head, int key)
{
if (head == null)
return null;
Node curr = head, prev = new Node();
while (curr.data != key) {
if (curr.next == head) {
System.out.printf("\nGiven node is not found"
+ " in the list!!!");
break;
}
prev = curr;
curr = curr.next;
}
if (curr == head && curr.next == head) {
head = null;
return head;
}
if (curr == head) {
prev = head;
while (prev.next != head)
prev = prev.next;
head = curr.next;
prev.next = head;
}
else if (curr.next == head) {
prev.next = head;
}
else {
prev.next = curr.next;
}
return head;
}
public static void main(String args[])
{
Node head = null;
head = push(head, 2);
head = push(head, 5);
head = push(head, 7);
head = push(head, 8);
head = push(head, 10);
System.out.printf("List Before Deletion: ");
printList(head);
head = deleteNode(head, 7);
System.out.printf("List After Deletion: ");
printList(head);
}
}
|
Python
class Node:
def __init__(self, next = None, data = None):
self.next = next
self.data = data
def push(head_ref, data):
ptr1 = Node()
ptr1.data = data
ptr1.next = head_ref
if (head_ref != None) :
temp = head_ref
while (temp.next != head_ref):
temp = temp.next
temp.next = ptr1
else:
ptr1.next = ptr1
head_ref = ptr1
return head_ref
def printList( head):
temp = head
if (head != None) :
while(True) :
print( temp.data, end = " ")
temp = temp.next
if (temp == head):
break
print()
def deleteNode( head, key) :
if (head == None):
return
if((head).data == key and (head).next == head):
head = None
last = head
d = None
if((head).data == key) :
while(last.next != head):
last = last.next
last.next = (head).next
head = last.next
while(last.next != head and last.next.data != key) :
last = last.next
if(last.next.data == key) :
d = last.next
last.next = d.next
else:
print("no such keyfound")
return head
head = None
head = push(head, 2)
head = push(head, 5)
head = push(head, 7)
head = push(head, 8)
head = push(head, 10)
print("List Before Deletion: ")
printList(head)
head = deleteNode(head, 7)
print( "List After Deletion: ")
printList(head)
|
C#
using System;
class GFG {
public class Node {
public int data;
public Node next;
};
static Node push(Node head_ref, int data)
{
Node ptr1 = new Node();
ptr1.data = data;
ptr1.next = head_ref;
if (head_ref != null)
{
Node temp = head_ref;
while (temp.next != head_ref)
temp = temp.next;
temp.next = ptr1;
}
else
ptr1.next = ptr1;
head_ref = ptr1;
return head_ref;
}
static void printList(Node head)
{
Node temp = head;
if (head != null)
{
do
{
Console.Write("{0} ", temp.data);
temp = temp.next;
} while (temp != head);
}
Console.Write("\n");
}
static Node deleteNode(Node head, int key)
{
if (head == null)
return null;
Node curr = head, prev = new Node();
while (curr.data != key)
{
if (curr.next == head)
{
Console.Write("\nGiven node is not found"
+ " in the list!!!");
break;
}
prev = curr;
curr = curr.next;
}
if (curr.next == head && curr == head)
{
head = null;
return head;
}
if (curr == head)
{
prev = head;
while (prev.next != head)
prev = prev.next;
head = curr.next;
prev.next = head;
}
else if (curr.next == head)
{
prev.next = head;
}
else
{
prev.next = curr.next;
}
return head;
}
public static void Main(String[] args)
{
Node head = null;
head = push(head, 2);
head = push(head, 5);
head = push(head, 7);
head = push(head, 8);
head = push(head, 10);
Console.Write("List Before Deletion: ");
printList(head);
head = deleteNode(head, 7);
Console.Write("List After Deletion: ");
printList(head);
}
}
|
Javascript
<script>
class Node {
constructor() {
this.data = 0;
this.next = null;
}
}
function push(head_ref , data) {
var ptr1 = new Node();
ptr1.data = data;
ptr1.next = head_ref;
if (head_ref != null) {
var temp = head_ref;
while (temp.next != head_ref)
temp = temp.next;
temp.next = ptr1;
} else
ptr1.next = ptr1;
head_ref = ptr1;
return head_ref;
}
function printList(head) {
var temp = head;
if (head != null) {
do {
document.write( temp.data+" ");
temp = temp.next;
} while (temp != head);
}
document.write("<br/>");
}
function deleteNode(head , key) {
if (head == null)
return null;
var curr = head, prev = new Node();
while (curr.data != key) {
if (curr.next == head) {
document.write("<br/>Given node is not found" + " in the list!!!");
break;
}
prev = curr;
curr = curr.next;
}
if (curr == head && curr.next == head) {
head = null;
return head;
}
if (curr == head) {
prev = head;
while (prev.next != head)
prev = prev.next;
head = curr.next;
prev.next = head;
}
else if (curr.next == head) {
prev.next = head;
} else {
prev.next = curr.next;
}
return head;
}
var head = null;
head = push(head, 2);
head = push(head, 5);
head = push(head, 7);
head = push(head, 8);
head = push(head, 10);
document.write("List Before Deletion: ");
printList(head);
head = deleteNode(head, 7);
document.write("List After Deletion: ");
printList(head);
</script>
|
OutputList Before Deletion: 10 8 7 5 2
List After Deletion: 10 8 5 2
Time Complexity: O(n), Worst case occurs when the element to be deleted is the last element and we need to move through the whole list.
Auxiliary Space: O(1), As constant extra space is used.
Last Updated :
13 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...