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

Given a Doubly 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:DLL = 18 <=> 15 <=> 8 <=> 9 <=> 14

Output:18 <=> 9 <=> 14

Explanation:

The linked list contains :

18 -> 1 + 8 = 9

15 -> 1 + 5 = 6

8 -> 8

9 -> 9

14 -> 1 + 4 = 5

Here, digit sum for nodes containing 15 and 8 are even.

Hence, these nodes have been deleted.

Input:DLL = 5 <=> 3 <=> 4 <=> 2 <=> 9

Output:5 <=> 3 <=> 9

Explanation:

The linked list contains two digit sum values 4 and 2.

Hence, these nodes have been deleted.

**Approach:**

A simple approach is to traverse the nodes of the doubly linked list one by one and for each node first, find the digit sum for the value present in the node by iterating through each digit and then finally remove the nodes whose digit sum is even.

Below is the implementation of the above approach:

## C++

`// C++ implementation to remove all ` `// the Even Digit Sum Nodes from a ` `// doubly linked list ` ` ` `#include <bits/stdc++.h> ` ` ` `using` `namespace` `std; ` ` ` `// Node of the doubly linked list ` `struct` `Node { ` ` ` `int` `data; ` ` ` `Node *prev, *next; ` `}; ` ` ` `// Function to insert a node at the beginning ` `// of the Doubly Linked List ` `void` `push(Node** head_ref, ` `int` `new_data) ` `{ ` ` ` `// Allocate the node ` ` ` `Node* new_node ` ` ` `= (Node*)` `malloc` `(` `sizeof` `(` `struct` `Node)); ` ` ` ` ` `// Insert the data ` ` ` `new_node->data = new_data; ` ` ` ` ` `// Since we are adding at the beginning, ` ` ` `// prev is always NULL ` ` ` `new_node->prev = NULL; ` ` ` ` ` `// Link the old list off the new node ` ` ` `new_node->next = (*head_ref); ` ` ` ` ` `// Change the prev of head node to new node ` ` ` `if` `((*head_ref) != NULL) ` ` ` `(*head_ref)->prev = new_node; ` ` ` ` ` `// Move the head to point to the new node ` ` ` `(*head_ref) = new_node; ` `} ` ` ` `// 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 a node ` `// in a Doubly Linked List. ` `// head_ref --> pointer to head node pointer. ` `// del --> pointer to node to be deleted ` `void` `deleteNode(Node** head_ref, Node* del) ` `{ ` ` ` `// Base case ` ` ` `if` `(*head_ref == NULL || del == NULL) ` ` ` `return` `; ` ` ` ` ` `// If the node to be deleted is head node ` ` ` `if` `(*head_ref == del) ` ` ` `*head_ref = del->next; ` ` ` ` ` `// Change next only if node to be ` ` ` `// deleted is not the last node ` ` ` `if` `(del->next != NULL) ` ` ` `del->next->prev = del->prev; ` ` ` ` ` `// Change prev only if node to be ` ` ` `// deleted is not the first node ` ` ` `if` `(del->prev != NULL) ` ` ` `del->prev->next = del->next; ` ` ` ` ` `// Finally, free the memory ` ` ` `// occupied by del ` ` ` `free` `(del); ` ` ` ` ` `return` `; ` `} ` ` ` `// Function to to remove all ` `// the Even Digit Sum Nodes from a ` `// doubly linked list ` `void` `deleteEvenDigitSumNodes(Node** head_ref) ` `{ ` ` ` `Node* ptr = *head_ref; ` ` ` `Node* next; ` ` ` ` ` `// Iterating through the linked list ` ` ` `while` `(ptr != NULL) { ` ` ` `next = ptr->next; ` ` ` ` ` `// If node's data's digit sum ` ` ` `// is even ` ` ` `if` `(!(digitSum(ptr->data) & 1)) ` ` ` `deleteNode(head_ref, ptr); ` ` ` ` ` `ptr = next; ` ` ` `} ` `} ` ` ` `// Function to print nodes in a ` `// given doubly linked list ` `void` `printList(Node* head) ` `{ ` ` ` `while` `(head != NULL) { ` ` ` `cout << head->data << ` `" "` `; ` ` ` `head = head->next; ` ` ` `} ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` ` ` `Node* head = NULL; ` ` ` ` ` `// Create the doubly linked list ` ` ` `// 18 <-> 15 <-> 8 <-> 9 <-> 14 ` ` ` `push(&head, 14); ` ` ` `push(&head, 9); ` ` ` `push(&head, 8); ` ` ` `push(&head, 15); ` ` ` `push(&head, 18); ` ` ` ` ` `cout << ` `"Original List: "` `; ` ` ` `printList(head); ` ` ` ` ` `deleteEvenDigitSumNodes(&head); ` ` ` ` ` `cout << ` `"\nModified List: "` `; ` ` ` `printList(head); ` `} ` |

*chevron_right*

*filter_none*

**Output:**

Original List: 18 15 8 9 14 Modified List: 18 9 14

* Time Complexity: O(N)*, where N is the total number of nodes.

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.

## Recommended Posts:

- Remove all nodes from a Doubly Linked List containing Fibonacci numbers
- Remove all even parity nodes from a Doubly and Circular Singly Linked List
- Remove all the Even Digit Sum Nodes from a Circular Singly Linked List
- Rotate Doubly linked list by N nodes
- Delete all the even nodes from a Doubly Linked List
- Remove duplicates from a sorted doubly linked list
- Remove duplicates from an unsorted doubly linked list
- Product of all prime nodes in a Doubly Linked List
- Delete all the nodes from the doubly linked list that are greater than a given value
- Sum of all nodes in a doubly linked list divisible by a given number K
- Delete all nodes from the doubly linked list which are divisible by K
- Delete all the nodes from a doubly linked list that are smaller than a given value
- Delete all Prime Nodes from a Doubly Linked List
- Replace even nodes of a doubly linked list with the elements of array
- Product of all nodes in a doubly linked list divisible by a given number K
- Sum and Product of all even digit sum Nodes of a Singly Linked List
- Sum and Product of nodes with value as even digit sum in Circular Linked List
- Remove all Fibonacci Nodes from a Circular Singly Linked List
- Difference between Singly linked list and Doubly linked list
- XOR Linked List – A Memory Efficient Doubly Linked List | Set 2

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.