Given a circular singly linked list containing N nodes. The task is to delete all nodes from the list which are prime.

**Examples:**

Input :9->11->32->6->13->20Output :Given List : 9 11 32 6 13 20 List After delete prime node : 9 32 6 20Input :6->11->16->21->17->10Output :Given List : 6 11 16 21 17 10 List After delete prime node : 10 21 16 6

**Approach:** The idea is to traverse the nodes of the circular singly linked list one by one and get the pointer of the nodes which are prime. Delete those nodes by following the approach used in the post: Delete a node from circular Linked List.

Below is the implementation of the above idea:

`// CPP program to delete all prime ` `// node from a Circular singly linked list ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Structure for a node ` `struct` `Node { ` ` ` `int` `data; ` ` ` `struct` `Node* next; ` `}; ` ` ` `// Function to insert a node at the begining ` `// of a Circular linked list ` `void` `push(` `struct` `Node** head_ref, ` `int` `data) ` `{ ` ` ` `struct` `Node* ptr1 = (` `struct` `Node*)` `malloc` `(` `sizeof` `(` `struct` `Node)); ` ` ` `struct` `Node* temp = *head_ref; ` ` ` `ptr1->data = data; ` ` ` `ptr1->next = *head_ref; ` ` ` ` ` `// If linked list is not NULL then ` ` ` `// set the next of last node ` ` ` `if` `(*head_ref != NULL) { ` ` ` `while` `(temp->next != *head_ref) ` ` ` `temp = temp->next; ` ` ` `temp->next = ptr1; ` ` ` `} ` ` ` `else` ` ` `ptr1->next = ptr1; ` `// For the first node ` ` ` ` ` `*head_ref = ptr1; ` `} ` ` ` `// Delete the node if it is prime ` `void` `deleteNode(Node* head_ref, Node* del) ` `{ ` ` ` `struct` `Node* temp = head_ref; ` ` ` `// If node to be deleted is head node ` ` ` ` ` `if` `(head_ref == del) ` ` ` `head_ref = del->next; ` ` ` ` ` `// travers list till not found ` ` ` `// delete node ` ` ` `while` `(temp->next != del) { ` ` ` `temp = temp->next; ` ` ` `} ` ` ` ` ` `// copy address of node ` ` ` `temp->next = del->next; ` ` ` ` ` `// Finally, free the memory ` ` ` `// occupied by del ` ` ` `free` `(del); ` ` ` ` ` `return` `; ` `} ` ` ` `// Function to check if a number is prime ` `bool` `isPrime(` `int` `n) ` `{ ` ` ` `// Corner cases ` ` ` `if` `(n <= 1) ` ` ` `return` `false` `; ` ` ` ` ` `if` `(n <= 3) ` ` ` `return` `true` `; ` ` ` ` ` `// This is checked so that we can skip ` ` ` `// middle five numbers in below loop ` ` ` `if` `(n % 2 == 0 || n % 3 == 0) ` ` ` `return` `false` `; ` ` ` ` ` `for` `(` `int` `i = 5; i * i <= n; i = i + 6) ` ` ` `if` `(n % i == 0 || n % (i + 2) == 0) ` ` ` `return` `false` `; ` ` ` ` ` `return` `true` `; ` `} ` ` ` `// Function to delete all prime nodes ` `// from the singly circular linked list ` `void` `deletePrimeNodes(Node* head) ` `{ ` ` ` `struct` `Node* ptr = head; ` ` ` ` ` `struct` `Node* next; ` ` ` ` ` `// traverse list till the endl ` ` ` `// if node is prime then delete it ` ` ` `do` `{ ` ` ` `// if node is prime ` ` ` `if` `(isPrime(ptr->data)) ` ` ` `deleteNode(head, ptr); ` ` ` ` ` `// point to next node ` ` ` `next = ptr->next; ` ` ` `ptr = next; ` ` ` ` ` `} ` `while` `(ptr != head); ` `} ` ` ` `// Function to print nodes in a ` `// given singly linked list ` `void` `printList(` `struct` `Node* head) ` `{ ` ` ` `struct` `Node* temp = head; ` ` ` `if` `(head != NULL) { ` ` ` `do` `{ ` ` ` `printf` `(` `"%d "` `, temp->data); ` ` ` `temp = temp->next; ` ` ` `} ` `while` `(temp != head); ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `// Initialize lists as empty ` ` ` `struct` `Node* head = NULL; ` ` ` ` ` `// Created linked list will be ` ` ` `// 9->11->32->6->13->20 ` ` ` `push(&head, 20); ` ` ` `push(&head, 13); ` ` ` `push(&head, 6); ` ` ` `push(&head, 32); ` ` ` `push(&head, 11); ` ` ` `push(&head, 9); ` ` ` ` ` `cout << ` `"Given List : "` `; ` ` ` `printList(head); ` ` ` ` ` `cout << ` `"\nList After deleting prime nodes : "` `; ` ` ` `deletePrimeNodes(head); ` ` ` `printList(head); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

Given List : 9 11 32 6 13 20 List After deleting prime nodes : 9 32 6 20

## Recommended Posts:

- Delete all Prime Nodes from a Singly Linked List
- Delete all Non-Prime Nodes from a Singly Linked List
- Sum and Product of the nodes of a Circular Singly Linked List which are divisible by K
- Delete all the even nodes of a Circular Linked List
- Delete all odd or even positioned nodes from Circular Linked List
- Sum and Product of all Prime Nodes of a Singly Linked List
- Count of Prime Nodes of a Singly Linked List
- Delete all Prime Nodes from a Doubly Linked List
- Circular Singly Linked List | Insertion
- Convert singly linked list into circular linked list
- Given only a pointer to a node to be deleted in a singly linked list, how do you delete it?
- Sum of the nodes of a Singly Linked List
- Given only a pointer/reference to a node to be deleted in a singly linked list, how do you delete it?
- Find minimum and maximum elements in singly Circular Linked List
- Delete every Kth node from circular linked list

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.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.