Remove all the Even Digit Sum 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 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 nodestruct Node {Â Â Â Â int data;Â Â Â Â struct Node* next;};Â
// Function to insert a node at the beginning// of a Circular linked listvoid 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 listvoid 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 numberint 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 listvoid 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 codeint 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 listimport java.util.*;  class GFG{  // Structure for a nodestatic class Node{    int data;    Node next;};  // Function to insert a node at the beginning// of a Circular linked liststatic 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 liststatic 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 numberstatic 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 liststatic 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 liststatic void printList(Node head){    Node temp = head;    if (head != null)     {        do        {            System.out.printf("%d ", temp.data);            temp = temp.next;        } while (temp != head);    }}  // Driver codepublic 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 nodeclass Node:         def __init__(self, data):                 self.data = data        self.next = None     # Function to insert a node at the beginning# of a Circular linked listdef 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 listdef 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 numberdef 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 listdef 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 listdef printList(head):Â
    temp = head         if (head != None):        while True:            print(temp.data, end = ' ')            temp = temp.next                         if (temp == head):                break         # Driver codeif __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 listusing System;class GFG{  // Structure for a nodeclass Node{  public int data;  public Node next;};  // Function to insert a node // at the beginning of a // Circular linked liststatic 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 liststatic 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 numberstatic 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 liststatic 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 liststatic void printList(Node head){  Node temp = head;  if (head != null)   {    do    {      Console.Write("{0} ",                     temp.data);      temp = temp.next;    } while (temp != head);  }}  // Driver codepublic 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 nodeclass 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 emptyvar 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> |
9 34 21
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)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



