# Remove all the Even Digit Sum Nodes from a Circular Singly Linked List

Given a circular singly linked list containing N nodes, the task is to remove all the nodes from the list which contains elements whose digit sum is even.

Examples:

Input: CLL = 9 -> 11 -> 34 -> 6 -> 13 -> 21
Output: 9 -> 34 -> 21
Explanation:
The circular singly linked list contains :
9 -> 9
11 -> 1 + 1 = 2
34 -> 3 + 4 = 7
6 -> 6
13 -> 1 + 3 = 4
21 -> 2 + 1 = 3
Here, digit sum for nodes containing 11, 6 and 13 are even.
Hence, these nodes have been deleted.

Input: 5 -> 11 -> 16 -> 21 -> 17 -> 10
Output: 5 -> 16 -> 21 -> 10
Explanation:
The linked list contains two digit sum values 11 and 17.
Hence, these nodes have been deleted.

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

Approach: The idea is to traverse the nodes of the circular singly linked list one by one and for each node, find the digit sum for the value present in the node by iterating through each digit. If the digit sum is even, then remove the nodes. Else, continue.

Below is the implementation of the above approach:

 `// C++ program to remove all ` `// the Even Digit Sum Nodes from a ` `// circular singly linked list ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Structure for a node ` `struct` `Node { ` `    ``int` `data; ` `    ``struct` `Node* next; ` `}; ` ` `  `// Function to insert a node at the beginning ` `// of a Circular linked list ` `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)); ` ` `  `    ``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) { ` ` `  `        ``// Find the node before head ` `        ``// and update next of it. ` `        ``while` `(temp->next != *head_ref) ` `            ``temp = temp->next; ` ` `  `        ``temp->next = ptr1; ` `    ``} ` `    ``else` ` `  `        ``// Point for the first node ` `        ``ptr1->next = ptr1; ` ` `  `    ``*head_ref = ptr1; ` `} ` ` `  `// Function to delete the node from a  ` `// Circular Linked list ` `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; ` ` `  `    ``// Traverse list till not found ` `    ``// delete node ` `    ``while` `(temp->next != del) { ` `        ``temp = temp->next; ` `    ``} ` ` `  `    ``// Copy the address of the node ` `    ``temp->next = del->next; ` ` `  `    ``// Finally, free the memory ` `    ``// occupied by del ` `    ``free``(del); ` ` `  `    ``return``; ` `} ` ` `  `// Function to find the digit sum ` `// for a number ` `int` `digitSum(``int` `num) ` `{ ` `    ``int` `sum = 0; ` `    ``while` `(num) { ` `        ``sum += (num % 10); ` `        ``num /= 10; ` `    ``} ` ` `  `    ``return` `sum; ` `} ` ` `  `// Function to delete all the Even Digit Sum Nodes ` `// from the singly circular linked list ` `void` `deleteEvenDigitSumNodes(Node* head) ` `{ ` `    ``struct` `Node* ptr = head; ` ` `  `    ``struct` `Node* next; ` ` `  `    ``// Traverse the list till the end ` `    ``do` `{ ` ` `  `        ``// If the node's data is Fibonacci, ` `        ``// delete node 'ptr' ` `        ``if` `(!(digitSum(ptr->data) & 1)) ` `            ``deleteNode(head, ptr); ` ` `  `        ``// Point to the next node ` `        ``next = ptr->next; ` `        ``ptr = next; ` ` `  `    ``} ``while` `(ptr != head); ` `} ` ` `  `// Function to print nodes in a ` `// given Circular 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->34->6->13->21 ` `    ``push(&head, 21); ` `    ``push(&head, 13); ` `    ``push(&head, 6); ` `    ``push(&head, 34); ` `    ``push(&head, 11); ` `    ``push(&head, 9); ` ` `  `    ``deleteEvenDigitSumNodes(head); ` ` `  `    ``printList(head); ` ` `  `    ``return` `0; ` `} `

Output:

```9 34 21
```

Time Complexity: O(KN), where N is the size of the linked list and K is the number of digits in the maximum number of the linked list.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.