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

• Last Updated : 18 Jun, 2021

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:
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:

Javascript



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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :