Remove all the Even Digit Sum Nodes from a Doubly Linked List
Last Updated :
12 Jul, 2025
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 of 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);
}
Java
// Java implementation to
// remove all the Even
// Digit Sum Nodes from a
// doubly linked list
import java.util.*;
class GFG{
// Node of the doubly
// linked list
static class Node
{
int data;
Node prev, next;
};
// Function to insert a node
// at the beginning of the
// Doubly Linked List
static Node push(Node head_ref,
int new_data)
{
// Allocate the node
Node new_node = new 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
// of 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;
return head_ref;
}
// Function to find the digit sum
// for a number
static int digitSum(int num)
{
int sum = 0;
while (num > 0)
{
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
static Node deleteNode(Node head_ref,
Node del)
{
// Base case
if (head_ref == null ||
del == null)
return head_ref;
// 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
del = null;
return head_ref;
}
// Function to to remove all
// the Even Digit Sum Nodes from a
// doubly linked list
static Node 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) % 2 == 1))
head_ref = deleteNode(head_ref, ptr);
ptr = next;
}
return head_ref;
}
// Function to print nodes in a
// given doubly linked list
static void printList(Node head)
{
while (head != null)
{
System.out.print(head.data + " ");
head = head.next;
}
}
// Driver Code
public static void main(String[] args)
{
Node head = null;
// Create the doubly linked list
// 18 <. 15 <. 8 <. 9 <. 14
head = push(head, 14);
head = push(head, 9);
head = push(head, 8);
head = push(head, 15);
head = push(head, 18);
System.out.print("Original List: ");
printList(head);
head = deleteEvenDigitSumNodes(head);
System.out.print("\nModified List: ");
printList(head);
}
}
// This code is contributed by shikhasingrajput
Python3
# Python3 implementation to remove all
# the Even Digit Sum Nodes from a
# doubly linked list
# Node of the doubly linked list
class Node():
def __init__(self):
self.data = 0
self.prev = None
self.next = None
# Function to insert a node at the
# beginning of the Doubly Linked List
def push(head_ref, new_data):
# Allocate the node
new_node = Node()
# Insert the data
new_node.data = new_data
# Since we are adding at the beginning,
# prev is always NULL
new_node.prev = None
# Link the old list of the new node
new_node.next = head_ref
# Change the prev of head node to new node
if ((head_ref) != None):
(head_ref).prev = new_node
# Move the head to point to the new node
(head_ref) = new_node
return head_ref
# Function to find the digit sum
# for a number
def digitSum(num):
sum = 0
while (num != 0):
sum += (num % 10)
num //= 10
return sum
# Function to delete a node
# in a Doubly Linked List.
# head_ref --> pointer to head node pointer.
# delete --> pointer to node to be deleted
def deleteNode(head_ref, delete):
# Base case
if (head_ref == None or delete == None):
return
# If the node to be deleted is head node
if (head_ref == delete):
head_ref = delete.next
# Change next only if node to be
# deleted is not the last node
if (delete.next != None):
delete.next.prev = delete.prev
# Change prev only if node to be
# deleted is not the first node
if (delete.prev != None):
delete.prev.next = delete.next
# Finally, free the memory
# occupied by delete
del delete
return
# Function to to remove all
# the Even Digit Sum Nodes from a
# doubly linked list
def deleteEvenDigitSumNodes(head_ref):
ptr = head_ref
next = None
# Iterating through the linked list
while (ptr != None):
next = ptr.next
# If node's data's digit sum
# is even
if (not (digitSum(ptr.data) & 1)):
deleteNode(head_ref, ptr)
ptr = next
return head_ref
# Function to print nodes in a
# given doubly linked list
def printList(head):
while (head != None):
print(head.data, end = " ")
head = head.next
# Driver code
if __name__=="__main__":
head = None
# Create the doubly linked list
# 18 <-> 15 <-> 8 <-> 9 <-> 14
head = push(head, 14)
head = push(head, 9)
head = push(head, 8)
head = push(head, 15)
head = push(head, 18)
print("Original List:", end = " ")
printList(head)
head = deleteEvenDigitSumNodes(head)
print("\nModified List: ", end = " ")
printList(head)
# This code is contributed by rutvik_56
C#
// C# implementation to
// remove all the Even
// Digit Sum Nodes from a
// doubly linked list
using System;
class GFG{
// Node of the doubly
// linked list
class Node
{
public int data;
public Node prev, next;
};
// Function to insert a node
// at the beginning of the
// Doubly Linked List
static Node push(Node head_ref,
int new_data)
{
// Allocate the node
Node new_node = new 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;
return head_ref;
}
// Function to find the digit sum
// for a number
static int digitSum(int num)
{
int sum = 0;
while (num > 0)
{
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
static Node deleteNode(Node head_ref,
Node del)
{
// Base case
if (head_ref == null ||
del == null)
return head_ref;
// 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
del = null;
return head_ref;
}
// Function to to remove all
// the Even Digit Sum Nodes from a
// doubly linked list
static Node 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) % 2 == 1))
head_ref = deleteNode(head_ref, ptr);
ptr = next;
}
return head_ref;
}
// Function to print nodes in a
// given doubly linked list
static void printList(Node head)
{
while (head != null)
{
Console.Write(head.data + " ");
head = head.next;
}
}
// Driver Code
public static void Main(String[] args)
{
Node head = null;
// Create the doubly linked list
// 18 <. 15 <. 8 <. 9 <. 14
head = push(head, 14);
head = push(head, 9);
head = push(head, 8);
head = push(head, 15);
head = push(head, 18);
Console.Write("Original List: ");
printList(head);
head = deleteEvenDigitSumNodes(head);
Console.Write("\nModified List: ");
printList(head);
}
}
// This code is contributed by shikhasingrajput
JavaScript
<script>
// Javascript implementation to remove all
// the Even Digit Sum Nodes from a
// doubly linked list
// Node of the doubly linked list
class Node {
constructor()
{
this.data = 0;
this.prev = null;
this.next = null;
}
};
// Function to insert a node at the beginning
// of the Doubly Linked List
function push(head_ref, new_data)
{
// Allocate the node
var new_node
= new 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 of 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;
return head_ref;
}
// Function to find the digit sum
// for a number
function digitSum(num)
{
var sum = 0;
while (num) {
sum += (num % 10);
num = parseInt(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
function deleteNode(head_ref, 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;
return;
}
// Function to to remove all
// the Even Digit Sum Nodes from a
// doubly linked list
function deleteEvenDigitSumNodes(head_ref)
{
var ptr = head_ref;
var 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;
}
return head_ref;
}
// Function to print nodes in a
// given doubly linked list
function printList(head)
{
while (head != null) {
document.write( head.data + " ");
head = head.next;
}
}
// Driver Code
var head = null;
// Create the doubly linked list
// 18 <. 15 <. 8 <. 9 <. 14
head = push(head, 14);
head = push(head, 9);
head = push(head, 8);
head = push(head, 15);
head = push(head, 18);
document.write( "Original List: ");
printList(head);
head = deleteEvenDigitSumNodes(head);
document.write( "<br>Modified List: ");
printList(head);
// This code is contributed by noob2000.
</script>
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.
Space Complexity: O(1) because using constant variables
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem