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++
// C++ program to remove all
// the Even Digit Sum Nodes from a
// circular singly linked list
#include <bits/stdc++.h>
using namespace std;
// Structure for a node
struct Node {
int data;
struct Node* next;
};
// Function to insert a node at the beginning
// of a Circular linked list
void push(struct Node** head_ref, int data)
{
// Create a new node and make head as next
// of it.
struct Node* ptr1
= (struct Node*)malloc(
sizeof(struct Node));
struct Node* temp = *head_ref;
ptr1->data = data;
ptr1->next = *head_ref;
// If linked list is not NULL then
// set the next of last node
if (*head_ref != NULL) {
// Find the node before head
// and update next of it.
while (temp->next != *head_ref)
temp = temp->next;
temp->next = ptr1;
}
else
// Point for the first node
ptr1->next = ptr1;
*head_ref = ptr1;
}
// Function to delete the node from a
// Circular Linked list
void deleteNode(Node* head_ref, Node* del)
{
struct Node* temp = head_ref;
// Traverse list till not found
// delete node
while (temp->next != del) {
temp = temp->next;
}
// Copy the address of the node
temp->next = del->next;
// Finally, free the memory
// occupied by del
free(del);
return;
}
// 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 all the Even Digit Sum Nodes
// from the singly circular linked list
Node* deleteEvenDigitSumNodes(Node* head)
{
struct Node* ptr = head;
struct Node* temp;
//setting up the head node to the first non even digit sum node
while(digitSum(ptr->data)%2==0){
ptr=ptr->next;
if(ptr==head){
return NULL;
}
}
temp = ptr;
Node* temp2=ptr;
ptr=ptr->next;
//setting up the last node next to the first non even digit sum node
while(ptr!=head){
temp2=ptr;
ptr=ptr->next;
}
temp2->next=temp;
head=temp;
ptr=head;
// traverse list till the endl
// if node is prime then delete it
do {
temp = ptr->next;
// if node is prime
if (digitSum(ptr->data)%2==0)
deleteNode(head, ptr);
ptr = temp;
} while (ptr != head);
return head;
}
// Function to print nodes in a
// given Circular linked list
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);
}
}
// Driver code
int main()
{
// Initialize lists as empty
struct Node* head = NULL;
// Created linked list will be
// 9->11->34->6->13->21
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
// Java program to remove all
// the Even Digit Sum Nodes from a
// circular singly linked list
import java.util.*;
class GFG{
// Structure for a node
static class Node
{
int data;
Node next;
};
// Function to insert a node at the beginning
// of a Circular linked list
static Node push(Node head_ref, int data)
{
// Create a new node and make head
// as next of it.
Node ptr1 = new Node();
Node temp = head_ref;
ptr1.data = data;
ptr1.next = head_ref;
// If linked list is not null then
// set the next of last node
if (head_ref != null)
{
// Find the node before head
// and update next of it.
while (temp.next != head_ref)
temp = temp.next;
temp.next = ptr1;
}
else
// Point for the first node
ptr1.next = ptr1;
head_ref = ptr1;
return head_ref;
}
// Function to delete the node from a
// Circular Linked list
static void deleteNode(Node head_ref, Node del)
{
Node temp = head_ref;
// If node to be deleted is head node
if (head_ref == del)
head_ref = del.next;
// Traverse list till not found
// delete node
while (temp.next != del)
{
temp = temp.next;
}
// Copy the address of the node
temp.next = del.next;
// Finally, free the memory
// occupied by del
del = null;
return;
}
// 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 all the Even Digit
// Sum Nodes from the singly circular
// linked list
static void deleteEvenDigitSumNodes(Node head)
{
Node ptr = head;
Node next;
// Traverse the list till the end
do
{
// If the node's data is Fibonacci,
// delete node 'ptr'
if (!(digitSum(ptr.data) % 2 == 1))
deleteNode(head, ptr);
// Point to the next node
next = ptr.next;
ptr = next;
} while (ptr != head);
}
// Function to print nodes in a
// given Circular linked list
static void printList(Node head)
{
Node temp = head;
if (head != null)
{
do
{
System.out.printf("%d ", temp.data);
temp = temp.next;
} while (temp != head);
}
}
// Driver code
public static void main(String[] args)
{
// Initialize lists as empty
Node head = null;
// Created linked list will be
// 9.11.34.6.13.21
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);
}
}
// This code is contributed by Amit Katiyar
Python3
# Python3 program to remove all
# the Even Digit Sum Nodes from a
# circular singly linked list
# Structure for a node
class Node:
def __init__(self, data):
self.data = data
self.next = None
# Function to insert a node at the beginning
# of a Circular linked list
def push(head_ref, data):
# Create a new node and make head as next
# of it.
ptr1 = Node(data)
temp = head_ref
ptr1.data = data
ptr1.next = head_ref
# If linked list is not None then
# set the next of last node
if (head_ref != None):
# Find the node before head
# and update next of it.
while (temp.next != head_ref):
temp = temp.next
temp.next = ptr1
else:
# Point for the first node
ptr1.next = ptr1
head_ref = ptr1
return head_ref
# Function to delete the node from a
# Circular Linked list
def deleteNode(head_ref, delt):
temp = head_ref
# If node to be deleted is head node
if (head_ref == delt):
head_ref = delt.next
# Traverse list till not found
# delete node
while (temp.next != delt):
temp = temp.next
# Copy the address of the node
temp.next = delt.next
# Finally, free the memory
# occupied by delt
del (delt)
return
# 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 all the Even Digit
# Sum Nodes from the singly circular linked list
def deleteEvenDigitSumNodes(head):
ptr = head
next = None
# Traverse the list till the end
while True:
# If the node's data is Fibonacci,
# delete node 'ptr'
if (not (digitSum(ptr.data) & 1)):
deleteNode(head, ptr)
# Point to the next node
next = ptr.next
ptr = next
if (ptr == head):
break
# Function to print nodes in a
# given Circular linked list
def printList(head):
temp = head
if (head != None):
while True:
print(temp.data, end = ' ')
temp = temp.next
if (temp == head):
break
# Driver code
if __name__=='__main__':
# Initialize lists as empty
head = None
# Created linked list will be
# 9.11.34.6.13.21
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)
# This code is contributed by rutvik_56
C#
// C# program to remove all
// the Even Digit Sum Nodes from a
// circular singly linked list
using System;
class GFG{
// Structure for a node
class Node
{
public int data;
public Node next;
};
// Function to insert a node
// at the beginning of a
// Circular linked list
static Node push(Node head_ref,
int data)
{
// Create a new node and make head
// as next of it.
Node ptr1 = new Node();
Node temp = head_ref;
ptr1.data = data;
ptr1.next = head_ref;
// If linked list is not null then
// set the next of last node
if (head_ref != null)
{
// Find the node before head
// and update next of it.
while (temp.next != head_ref)
temp = temp.next;
temp.next = ptr1;
}
else
// Point for the first node
ptr1.next = ptr1;
head_ref = ptr1;
return head_ref;
}
// Function to delete the node from a
// Circular Linked list
static void deleteNode(Node head_ref,
Node del)
{
Node temp = head_ref;
// If node to be deleted is head node
if (head_ref == del)
head_ref = del.next;
// Traverse list till not found
// delete node
while (temp.next != del)
{
temp = temp.next;
}
// Copy the address of the node
temp.next = del.next;
// Finally, free the memory
// occupied by del
del = null;
return;
}
// 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 all the Even Digit
// Sum Nodes from the singly circular
// linked list
static void deleteEvenDigitSumNodes(Node head)
{
Node ptr = head;
Node next;
// Traverse the list till the end
do
{
// If the node's data is Fibonacci,
// delete node 'ptr'
if (!(digitSum(ptr.data) % 2 == 1))
deleteNode(head, ptr);
// Point to the next node
next = ptr.next;
ptr = next;
} while (ptr != head);
}
// Function to print nodes in a
// given Circular linked list
static void printList(Node head)
{
Node temp = head;
if (head != null)
{
do
{
Console.Write("{0} ",
temp.data);
temp = temp.next;
} while (temp != head);
}
}
// Driver code
public static void Main(String[] args)
{
// Initialize lists as empty
Node head = null;
// Created linked list will be
// 9.11.34.6.13.21
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);
}
}
// This code is contributed by 29AjayKumar
JavaScript
<script>
// javascript program to remove all
// the Even Digit Sum Nodes from a
// circular singly linked list
// Structure for a node
class Node {
constructor() {
this.data = 0;
this.next = null;
}
}
// Function to insert a node at the beginning
// of a Circular linked list
function push(head_ref , data) {
// Create a new node and make head
// as next of it.
var ptr1 = new Node();
var temp = head_ref;
ptr1.data = data;
ptr1.next = head_ref;
// If linked list is not null then
// set the next of last node
if (head_ref != null) {
// Find the node before head
// and update next of it.
while (temp.next != head_ref)
temp = temp.next;
temp.next = ptr1;
} else
// Point for the first node
ptr1.next = ptr1;
head_ref = ptr1;
return head_ref;
}
// Function to delete the node from a
// Circular Linked list
function deleteNode(head_ref, del) {
var temp = head_ref;
// If node to be deleted is head node
if (head_ref == del)
head_ref = del.next;
// Traverse list till not found
// delete node
while (temp.next != del) {
temp = temp.next;
}
// Copy the address of the node
temp.next = del.next;
// Finally, free the memory
// occupied by del
del = null;
return;
}
// Function to find the digit sum
// for a number
function digitSum(num) {
var sum = 0;
while (num > 0) {
sum += (num % 10);
num = parseInt(num/10);
}
return sum;
}
// Function to delete all the Even Digit
// Sum Nodes from the singly circular
// linked list
function deleteEvenDigitSumNodes(head) {
var ptr = head;
var next;
// Traverse the list till the end
do {
// If the node's data is Fibonacci,
// delete node 'ptr'
if (!(digitSum(ptr.data) % 2 == 1))
deleteNode(head, ptr);
// Point to the next node
next = ptr.next;
ptr = next;
} while (ptr != head);
}
// Function to print nodes in a
// given Circular linked list
function printList(head) {
var temp = head;
if (head != null) {
do {
document.write( temp.data+" ");
temp = temp.next;
} while (temp != head);
}
}
// Driver code
// Initialize lists as empty
var head = null;
// Created linked list will be
// 9.11.34.6.13.21
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);
// This code contributed by aashish1995
</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)
Similar Reads
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
13 min read
Remove all Fibonacci Nodes from a Circular Singly Linked List
Given a circular singly linked list containing N nodes, the task is to remove all the nodes from the list which contains Fibonacci data values.Examples: Input: CLL = 9 -> 11 -> 34 -> 6 -> 13 -> 20 Output: 9 -> 11 -> 6 -> 20 Explanation: The list contains 2 fibonacci data valu
15+ min read
Delete all Prime Nodes from a Circular Singly Linked List
Given a circular singly linked list containing N nodes. The task is to delete all nodes from the list which are prime. Examples: Input : 9->11->32->6->13->20 Output : 9 32 6 20 Input : 6->11->16->21->17->10 Output : 6 16 21 10 Approach: The idea is to traverse the nodes
13 min read
Remove all even parity nodes from a Doubly and Circular Singly Linked List
Given a Doubly linked list and Circular singly linked list containing N nodes, the task is to remove all the nodes from each list which contains elements whose parity is even. Example: Input: CLL = 9 -> 11 -> 34 -> 6 -> 13 -> 21 Output: 11 -> 13 -> 21 Explanation: The circular s
15+ min read
Delete all the even nodes of a Circular Linked List
Given a circular singly linked list containing N nodes, the task is to delete all the even nodes from the list. Examples: Input : 57->11->2->56->12->61 Output : List after deletion : 57 -> 11 -> 61 Input : 9->11->32->6->13->20 Output : List after deletion : 9 -
10 min read
Program to delete all even nodes from a Singly Linked List
Given a singly linked list containing N nodes, the task is to delete all the even nodes from the list. Examples: Input: LL = 1 -> 4 -> 3 -> 18 -> 19 Output: 1 -> 3 -> 19Input: LL = 5 -> 3 -> 6 -> 8 -> 4 -> 1 -> 2 -> 9 Output: 5 -> 3 -> 1 -> 9 Approach
15+ min read
Delete all odd or even positioned nodes from Circular Linked List
Delete all odd or even position nodes from circular linked list Given a Singly Circular Linked List, starting from the first node delete all odd position nodes in it. Note: Linked list is considered to have 1-based indexing. That is, first element in the linked list is at position 1. Examples:  In
15+ min read
Sum and Product of all even digit sum Nodes of a Singly Linked List
Given a singly linked list containing N nodes, the task is to find the sum and product of all the nodes from the list whose data value has an even digit sum. Examples: Input: 15 -> 16 -> 8 -> 6 -> 13 Output: Sum = 42 Product = 9360 Explanation: The sum of all digit of number in linked li
15+ min read
Sum and Product of nodes with value as even digit sum in Circular Linked List
Given a circular singly linked list containing N nodes, The task is to find the sum and product of all the nodes from the list whose data value has an even digit sum. Examples: Input: List = 15 -> 16 -> 8 -> 6 -> 13 Output: Sum = 42, Product = 9360 Explanation: The circular linked list c
11 min read
Delete all Prime Nodes from a Singly Linked List
Given a singly linked list containing N nodes, the task is to delete all nodes from the list which are prime.Examples: Input : List = 15 -> 16 -> 6 -> 7 -> 17 Output : Final List = 15 -> 16 -> 6Input : List = 15 -> 3 -> 4 -> 2 -> 9 Output :Final List = 15 ->4 -> 9
12 min read