Remove all the Even Digit Sum Nodes from a Circular Singly Linked List
Last Updated :
22 Apr, 2022
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.
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++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};
void push( struct Node** head_ref, int data)
{
struct Node* ptr1
= ( struct Node*) malloc (
sizeof ( struct Node));
struct Node* temp = *head_ref;
ptr1->data = data;
ptr1->next = *head_ref;
if (*head_ref != NULL) {
while (temp->next != *head_ref)
temp = temp->next;
temp->next = ptr1;
}
else
ptr1->next = ptr1;
*head_ref = ptr1;
}
void deleteNode(Node* head_ref, Node* del)
{
struct Node* temp = head_ref;
while (temp->next != del) {
temp = temp->next;
}
temp->next = del->next;
free (del);
return ;
}
int digitSum( int num)
{
int sum = 0;
while (num) {
sum += (num % 10);
num /= 10;
}
return sum;
}
Node* deleteEvenDigitSumNodes(Node* head)
{
struct Node* ptr = head;
struct Node* temp;
while (digitSum(ptr->data)%2==0){
ptr=ptr->next;
if (ptr==head){
return NULL;
}
}
temp = ptr;
Node* temp2=ptr;
ptr=ptr->next;
while (ptr!=head){
temp2=ptr;
ptr=ptr->next;
}
temp2->next=temp;
head=temp;
ptr=head;
do {
temp = ptr->next;
if (digitSum(ptr->data)%2==0)
deleteNode(head, ptr);
ptr = temp;
} while (ptr != head);
return head;
}
void printList( struct Node* head)
{
if (!head) printf ( "NULL" );
struct Node* temp = head;
if (head != NULL) {
do {
printf ( "%d " , temp->data);
temp = temp->next;
} while (temp != head);
}
}
int main()
{
struct Node* head = NULL;
push(&head, 21);
push(&head, 13);
push(&head, 6);
push(&head, 34);
push(&head, 11);
push(&head, 9);
head = deleteEvenDigitSumNodes(head);
printList(head);
return 0;
}
|
Java
import java.util.*;
class GFG{
static class Node
{
int data;
Node next;
};
static Node push(Node head_ref, int data)
{
Node ptr1 = new Node();
Node temp = head_ref;
ptr1.data = data;
ptr1.next = head_ref;
if (head_ref != null )
{
while (temp.next != head_ref)
temp = temp.next;
temp.next = ptr1;
}
else
ptr1.next = ptr1;
head_ref = ptr1;
return head_ref;
}
static void deleteNode(Node head_ref, Node del)
{
Node temp = head_ref;
if (head_ref == del)
head_ref = del.next;
while (temp.next != del)
{
temp = temp.next;
}
temp.next = del.next;
del = null ;
return ;
}
static int digitSum( int num)
{
int sum = 0 ;
while (num > 0 )
{
sum += (num % 10 );
num /= 10 ;
}
return sum;
}
static void deleteEvenDigitSumNodes(Node head)
{
Node ptr = head;
Node next;
do
{
if (!(digitSum(ptr.data) % 2 == 1 ))
deleteNode(head, ptr);
next = ptr.next;
ptr = next;
} while (ptr != head);
}
static void printList(Node head)
{
Node temp = head;
if (head != null )
{
do
{
System.out.printf( "%d " , temp.data);
temp = temp.next;
} while (temp != head);
}
}
public static void main(String[] args)
{
Node head = null ;
head = push(head, 21 );
head = push(head, 13 );
head = push(head, 6 );
head = push(head, 34 );
head = push(head, 11 );
head = push(head, 9 );
deleteEvenDigitSumNodes(head);
printList(head);
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = None
def push(head_ref, data):
ptr1 = Node(data)
temp = head_ref
ptr1.data = data
ptr1. next = head_ref
if (head_ref ! = None ):
while (temp. next ! = head_ref):
temp = temp. next
temp. next = ptr1
else :
ptr1. next = ptr1
head_ref = ptr1
return head_ref
def deleteNode(head_ref, delt):
temp = head_ref
if (head_ref = = delt):
head_ref = delt. next
while (temp. next ! = delt):
temp = temp. next
temp. next = delt. next
del (delt)
return
def digitSum(num):
sum = 0
while (num ! = 0 ):
sum + = (num % 10 )
num / / = 10
return sum
def deleteEvenDigitSumNodes(head):
ptr = head
next = None
while True :
if ( not (digitSum(ptr.data) & 1 )):
deleteNode(head, ptr)
next = ptr. next
ptr = next
if (ptr = = head):
break
def printList(head):
temp = head
if (head ! = None ):
while True :
print (temp.data, end = ' ' )
temp = temp. next
if (temp = = head):
break
if __name__ = = '__main__' :
head = None
head = push(head, 21 )
head = push(head, 13 )
head = push(head, 6 )
head = push(head, 34 )
head = push(head, 11 )
head = push(head, 9 )
deleteEvenDigitSumNodes(head)
printList(head)
|
C#
using System;
class GFG{
class Node
{
public int data;
public Node next;
};
static Node push(Node head_ref,
int data)
{
Node ptr1 = new Node();
Node temp = head_ref;
ptr1.data = data;
ptr1.next = head_ref;
if (head_ref != null )
{
while (temp.next != head_ref)
temp = temp.next;
temp.next = ptr1;
}
else
ptr1.next = ptr1;
head_ref = ptr1;
return head_ref;
}
static void deleteNode(Node head_ref,
Node del)
{
Node temp = head_ref;
if (head_ref == del)
head_ref = del.next;
while (temp.next != del)
{
temp = temp.next;
}
temp.next = del.next;
del = null ;
return ;
}
static int digitSum( int num)
{
int sum = 0;
while (num > 0)
{
sum += (num % 10);
num /= 10;
}
return sum;
}
static void deleteEvenDigitSumNodes(Node head)
{
Node ptr = head;
Node next;
do
{
if (!(digitSum(ptr.data) % 2 == 1))
deleteNode(head, ptr);
next = ptr.next;
ptr = next;
} while (ptr != head);
}
static void printList(Node head)
{
Node temp = head;
if (head != null )
{
do
{
Console.Write( "{0} " ,
temp.data);
temp = temp.next;
} while (temp != head);
}
}
public static void Main(String[] args)
{
Node head = null ;
head = push(head, 21);
head = push(head, 13);
head = push(head, 6);
head = push(head, 34);
head = push(head, 11);
head = push(head, 9);
deleteEvenDigitSumNodes(head);
printList(head);
}
}
|
Javascript
<script>
class Node {
constructor() {
this .data = 0;
this .next = null ;
}
}
function push(head_ref , data) {
var ptr1 = new Node();
var temp = head_ref;
ptr1.data = data;
ptr1.next = head_ref;
if (head_ref != null ) {
while (temp.next != head_ref)
temp = temp.next;
temp.next = ptr1;
} else
ptr1.next = ptr1;
head_ref = ptr1;
return head_ref;
}
function deleteNode(head_ref, del) {
var temp = head_ref;
if (head_ref == del)
head_ref = del.next;
while (temp.next != del) {
temp = temp.next;
}
temp.next = del.next;
del = null ;
return ;
}
function digitSum(num) {
var sum = 0;
while (num > 0) {
sum += (num % 10);
num = parseInt(num/10);
}
return sum;
}
function deleteEvenDigitSumNodes(head) {
var ptr = head;
var next;
do {
if (!(digitSum(ptr.data) % 2 == 1))
deleteNode(head, ptr);
next = ptr.next;
ptr = next;
} while (ptr != head);
}
function printList(head) {
var temp = head;
if (head != null ) {
do {
document.write( temp.data+ " " );
temp = temp.next;
} while (temp != head);
}
}
var head = null ;
head = push(head, 21);
head = push(head, 13);
head = push(head, 6);
head = push(head, 34);
head = push(head, 11);
head = push(head, 9);
deleteEvenDigitSumNodes(head);
printList(head);
</script>
|
Time Complexity: O(KN), where N is the size of the linked list and K is the maximum number of digits in any node.
Auxiliary Space: (1)
Share your thoughts in the comments
Please Login to comment...