Deletion from a Circular Linked List

Traversal in a circular linked list

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

7
```

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 curr to next node, everytime 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 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 = temp -> next and delete curr.

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Complete C program to demonstrate deletion in Circular Linked List:

```// C program to delete a given key from
#include<stdio.h>
#include<stdlib.h>

/* structure for a node */
struct Node
{
int data;
struct Node *next;
};

/* Function to insert a node at the beginning of
void push(struct Node **head_ref, int data)
{
// Create a new node and make head as next
// of it.
struct Node *ptr1 =
(struct Node *)malloc(sizeof(struct Node));
ptr1->data = data;

/* If linked list is not NULL then set the
next of last node */
{
// Find the node before head and update
// next of it.
temp = temp->next;
temp->next = ptr1;
}
else
ptr1->next = ptr1; /*For the first node */

}

/* Function to print nodes in a given
{
{
do
{
printf("%d ", temp->data);
temp = temp->next;
}
}

printf("\n");
}

/* Function to delete a given node from the list */
void deleteNode(struct Node *head, int key)
{
return;

// Find the required node
struct Node *curr = head, *prev;
while (curr->data != key)
{
{
" in the list!!!");
break;
}

prev = curr;
curr = curr -> next;
}

// Check if node is only node
{
free(curr);
return;
}

// If more than one node, check if
// it is first node
{
while (prev -> next != head)
prev = prev -> next;
free(curr);
}

// check if node is last node
else if (curr -> next == head)
{
free(curr);
}
else
{
prev->next = curr->next;
free(curr);
}
}

/* Driver program to test above functions */
int main()
{
/* Initialize lists as empty */

/* Created linked list will be 2->5->7->8->10 */

printf("List Before Deletion: ");

printf("List After Deletion: ");

return 0;
}
```

Output:

```List Before Deletion: 10 8 7 5 2
List After Deletion: 10 8 5 2
```

This article is contributed by Harsh Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

GATE CS Corner    Company Wise Coding Practice

1.4 Average Difficulty : 1.4/5.0
Based on 5 vote(s)