Remove all the Even Digit Sum Nodes from a Doubly Linked List
Last Updated :
16 Feb, 2023
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++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node *prev, *next;
};
void push(Node** head_ref, int new_data)
{
Node* new_node
= (Node*) malloc ( sizeof ( struct Node));
new_node->data = new_data;
new_node->prev = NULL;
new_node->next = (*head_ref);
if ((*head_ref) != NULL)
(*head_ref)->prev = new_node;
(*head_ref) = new_node;
}
int digitSum( int num)
{
int sum = 0;
while (num) {
sum += (num % 10);
num /= 10;
}
return sum;
}
void deleteNode(Node** head_ref, Node* del)
{
if (*head_ref == NULL || del == NULL)
return ;
if (*head_ref == del)
*head_ref = del->next;
if (del->next != NULL)
del->next->prev = del->prev;
if (del->prev != NULL)
del->prev->next = del->next;
free (del);
return ;
}
void deleteEvenDigitSumNodes(Node** head_ref)
{
Node* ptr = *head_ref;
Node* next;
while (ptr != NULL) {
next = ptr->next;
if (!(digitSum(ptr->data) & 1))
deleteNode(head_ref, ptr);
ptr = next;
}
}
void printList(Node* head)
{
while (head != NULL) {
cout << head->data << " " ;
head = head->next;
}
}
int main()
{
Node* head = NULL;
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
import java.util.*;
class GFG{
static class Node
{
int data;
Node prev, next;
};
static Node push(Node head_ref,
int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.prev = null ;
new_node.next = head_ref;
if (head_ref != null )
head_ref.prev = new_node;
head_ref = new_node;
return head_ref;
}
static int digitSum( int num)
{
int sum = 0 ;
while (num > 0 )
{
sum += (num % 10 );
num /= 10 ;
}
return sum;
}
static Node deleteNode(Node head_ref,
Node del)
{
if (head_ref == null ||
del == null )
return head_ref;
if (head_ref == del)
head_ref = del.next;
if (del.next != null )
del.next.prev = del.prev;
if (del.prev != null )
del.prev.next = del.next;
del = null ;
return head_ref;
}
static Node deleteEvenDigitSumNodes(Node head_ref)
{
Node ptr = head_ref;
Node next;
while (ptr != null )
{
next = ptr.next;
if (!(digitSum(ptr.data) % 2 == 1 ))
head_ref = deleteNode(head_ref, ptr);
ptr = next;
}
return head_ref;
}
static void printList(Node head)
{
while (head != null )
{
System.out.print(head.data + " " );
head = head.next;
}
}
public static void main(String[] args)
{
Node head = null ;
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);
}
}
|
Python3
class Node():
def __init__( self ):
self .data = 0
self .prev = None
self . next = None
def push(head_ref, new_data):
new_node = Node()
new_node.data = new_data
new_node.prev = None
new_node. next = head_ref
if ((head_ref) ! = None ):
(head_ref).prev = new_node
(head_ref) = new_node
return head_ref
def digitSum(num):
sum = 0
while (num ! = 0 ):
sum + = (num % 10 )
num / / = 10
return sum
def deleteNode(head_ref, delete):
if (head_ref = = None or delete = = None ):
return
if (head_ref = = delete):
head_ref = delete. next
if (delete. next ! = None ):
delete. next .prev = delete.prev
if (delete.prev ! = None ):
delete.prev. next = delete. next
del delete
return
def deleteEvenDigitSumNodes(head_ref):
ptr = head_ref
next = None
while (ptr ! = None ):
next = ptr. next
if ( not (digitSum(ptr.data) & 1 )):
deleteNode(head_ref, ptr)
ptr = next
return head_ref
def printList(head):
while (head ! = None ):
print (head.data, end = " " )
head = head. next
if __name__ = = "__main__" :
head = None
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)
|
C#
using System;
class GFG{
class Node
{
public int data;
public Node prev, next;
};
static Node push(Node head_ref,
int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.prev = null ;
new_node.next = head_ref;
if (head_ref != null )
head_ref.prev = new_node;
head_ref = new_node;
return head_ref;
}
static int digitSum( int num)
{
int sum = 0;
while (num > 0)
{
sum += (num % 10);
num /= 10;
}
return sum;
}
static Node deleteNode(Node head_ref,
Node del)
{
if (head_ref == null ||
del == null )
return head_ref;
if (head_ref == del)
head_ref = del.next;
if (del.next != null )
del.next.prev = del.prev;
if (del.prev != null )
del.prev.next = del.next;
del = null ;
return head_ref;
}
static Node deleteEvenDigitSumNodes(Node head_ref)
{
Node ptr = head_ref;
Node next;
while (ptr != null )
{
next = ptr.next;
if (!(digitSum(ptr.data) % 2 == 1))
head_ref = deleteNode(head_ref, ptr);
ptr = next;
}
return head_ref;
}
static void printList(Node head)
{
while (head != null )
{
Console.Write(head.data + " " );
head = head.next;
}
}
public static void Main(String[] args)
{
Node head = null ;
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);
}
}
|
Javascript
<script>
class Node {
constructor()
{
this .data = 0;
this .prev = null ;
this .next = null ;
}
};
function push(head_ref, new_data)
{
var new_node
= new Node();
new_node.data = new_data;
new_node.prev = null ;
new_node.next = (head_ref);
if ((head_ref) != null )
(head_ref).prev = new_node;
(head_ref) = new_node;
return head_ref;
}
function digitSum(num)
{
var sum = 0;
while (num) {
sum += (num % 10);
num = parseInt(num/10);
}
return sum;
}
function deleteNode(head_ref, del)
{
if (head_ref == null || del == null )
return ;
if (head_ref == del)
head_ref = del.next;
if (del.next != null )
del.next.prev = del.prev;
if (del.prev != null )
del.prev.next = del.next;
return ;
}
function deleteEvenDigitSumNodes(head_ref)
{
var ptr = head_ref;
var next;
while (ptr != null ) {
next = ptr.next;
if (!(digitSum(ptr.data) & 1))
deleteNode(head_ref, ptr);
ptr = next;
}
return head_ref;
}
function printList(head)
{
while (head != null ) {
document.write( head.data + " " );
head = head.next;
}
}
var head = null ;
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);
</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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...