Posted on by Kalkicode

# Remove all the Even digit sum nodes from a circular linked list

The problem involves removing nodes from a circular linked list based on whether the sum of the digits in their data is even. If the sum of digits is even, the node is removed from the list; otherwise, it is retained. This problem demonstrates how to manipulate circular linked lists and evaluate digit sums.

## Problem Statement

Given a circular linked list, the problem is to remove all nodes from the list whose data has an even digit sum.

## Example

Linked List: 13 -> 12 -> 32 -> 1 -> 14 -> 91 -> 27 -> 124 -> 88

After removing nodes with even digit sum:

Result: 12 -> 32 -> 1 -> 14 -> 27 -> 124

## Idea to Solve the Problem

To solve the problem of removing nodes with even digit sums from a circular linked list, follow these steps:

1. Initialize pointers `temp` and `auxiliary` to the head node of the circular linked list.
2. Traverse the circular linked list using the `temp` pointer.
3. For each node, compute the sum of its digits using the `digitSum` function.
4. If the sum is even, remove the current node using the following logic:
• Update the `auxiliary` pointer to point to the next node.
• Update the next pointer of the previous node to skip the current node.
• If the current node is the head node, update the head pointer to the next node.
• If the current node is the tail node, update the tail pointer to the previous node.
• Free the memory allocated for the removed node.
5. If the sum is odd, move the `auxiliary` pointer to the current node and continue to the next node.
6. Continue this process until the traversal completes and the `temp` pointer points back to the head node.

## Pseudocode

``````function removeEvenDigitSumNode():
return
point = null
while temp is not null:
sum = digitSum(temp.data)
if (sum & 1) is equal to 0:
auxiliary = temp
temp = temp.next
if auxiliary is equal to head:
if auxiliary is equal to tail:
tail = null
temp = null
point = null
else:
tail.next = temp
else if auxiliary is equal to tail:
if point is not null:
tail = point
else:
if point is not null:
point.next = temp
auxiliary = null
else:
point = temp
temp = temp.next
if temp is equal to head:
temp = null``````

## Algorithm Explanation

1. If the circular linked list is empty, return.
2. Initialize pointers `temp` and `auxiliary` to the head node of the circular linked list. Also, initialize `point` to null.
3. Traverse the circular linked list using the `temp` pointer.
4. For each node, compute the sum of its digits using the `digitSum` function.
5. If the sum is even, remove the current node:
• Update the `auxiliary` pointer to point to the next node.
• Update the next pointer of the previous node to skip the current node.
• If the current node is the head node, update the head pointer to the next node.
• If the current node is the tail node, update the tail pointer to the previous node.
• Free the memory allocated for the removed node.
6. If the sum is odd, move the `auxiliary` pointer to the current node and continue to the next node.
7. Continue this process until the traversal completes and the `temp` pointer points back to the head node.

## Code Solution

``````// Java Program
// Remove all the Even digit sum nodes from a circular linked list
{
public int data;
{
this.data = data;
this.next = null;
}
}
{
{
// Set initial value
this.tail = null;
}
public void insert(int value)
{
{
}
else
{
this.tail.next = node;
}
this.tail = node;
}
// Display node element of circular linked list
public void display()
{
{
}
else
{
// First node of linked list
while (temp != null && temp != this.head)
{
// Display node value
System.out.print("  " + temp.data);
// visit to next node
temp = temp.next;
}
}
}
public int digitSum(int num)
{
int n = num;
int sum = 0;
if (n < 0)
{
n = -n;
}
while (n > 0)
{
sum += (n % 10);
n = n / 10;
}
return sum;
}
public void removeEvenDigitSumNode()
{
{
return;
}
while (temp != null)
{
if ((digitSum(temp.data) & 1) == 0)
{
auxiliary = temp;
temp = temp.next;
{
if (auxiliary == this.tail)
{
// When Remove last node
this.tail = null;
temp = null;
point = null;
}
else
{
this.tail.next = temp;
}
}
else if (auxiliary == this.tail)
{
// When Remove last node
if (point != null)
{
}
this.tail = point;
}
else
{
// When removing intermediate node
if (point != null)
{
point.next = temp;
}
}
auxiliary = null;
}
else
{
point = temp;
// visit to next node
temp = temp.next;
{
// Stop the process
temp = null;
}
}
}
}
public static void main(String[] args)
{
// Insert node
cll.insert(13);
cll.insert(12);
cll.insert(32);
cll.insert(1);
cll.insert(14);
cll.insert(91);
cll.insert(27);
cll.insert(124);
cll.insert(88);
System.out.println("\n Before remove even digit sum nodes");
cll.display();
/*

Node   Digit Sum    Even
----- --------    --------
13    [1+3] 4       Yes
12    [1+2] 3       No
32    [3+2] 5       No
1     [1]   1       No
14    [1+4] 5       No
91    [9+1] 10      Yes
27    [2+7] 9       No
124   [1+2+4] 7     No
88    [8+8] 16      Yes
--------------------------
Result
------
12 → 32 → 1 → 14 → 27 → 124 ┐
└-------------------------⤶
*/
cll.removeEvenDigitSumNode();
System.out.println("\n After remove even digit sum nodes");
cll.display();
}
}``````

#### Output

`````` Before remove even digit sum nodes
13  12  32  1  14  91  27  124  88
After remove even digit sum nodes
12  32  1  14  27  124``````
``````// Include header file
#include <iostream>
using namespace std;
// C++ Program
// Remove all the Even digit sum nodes from a circular linked list
{
public: int data;
{
this->data = data;
this->next = NULL;
}
};
{
{
this->tail = NULL;
}
void insert(int value)
{
{
}
else
{
this->tail->next = node;
}
this->tail = node;
}
// Display node element of circular linked list
void display()
{
{
cout << "\n Empty Linked List" << endl;
}
else
{
// First node of linked list
cout << "  " << this->head->data;
while (temp != NULL && temp != this->head)
{
// Display node value
cout << "  " << temp->data;
// visit to next node
temp = temp->next;
}
}
}
int digitSum(int num)
{
int n = num;
int sum = 0;
if (n < 0)
{
n = -n;
}
while (n > 0)
{
sum += (n % 10);
n = n / 10;
}
return sum;
}
void removeEvenDigitSumNode()
{
{
return;
}
while (temp != NULL)
{
if ((this->digitSum(temp->data) &1) == 0)
{
auxiliary = temp;
temp = temp->next;
{
if (auxiliary == this->tail)
{
// When Remove last node
this->tail = NULL;
temp = NULL;
point = NULL;
}
else
{
this->tail->next = temp;
}
}
else if (auxiliary == this->tail)
{
// When Remove last node
if (point != NULL)
{
}
this->tail = point;
}
else
{
// When removing intermediate node
if (point != NULL)
{
point->next = temp;
}
}
delete auxiliary;
auxiliary = NULL;
}
else
{
point = temp;
// visit to next node
temp = temp->next;
{
// Stop the process
temp = NULL;
}
}
}
}
};
int main()
{
// Insert node
cll->insert(13);
cll->insert(12);
cll->insert(32);
cll->insert(1);
cll->insert(14);
cll->insert(91);
cll->insert(27);
cll->insert(124);
cll->insert(88);
cout << "\n Before remove even digit sum nodes" << endl;
cll->display();
/*
Node   Digit Sum    Even
----- --------    --------
13    [1+3] 4       Yes
12    [1+2] 3       No
32    [3+2] 5       No
1     [1]   1       No
14    [1+4] 5       No
91    [9+1] 10      Yes
27    [2+7] 9       No
124   [1+2+4] 7     No
88    [8+8] 16      Yes
--------------------------
Result
------
12 → 32 → 1 → 14 → 27 → 124 ┐
└-------------------------⤶
*/
cll->removeEvenDigitSumNode();
cout << "\n After remove even digit sum nodes" << endl;
cll->display();
return 0;
}``````

#### Output

`````` Before remove even digit sum nodes
13  12  32  1  14  91  27  124  88
After remove even digit sum nodes
12  32  1  14  27  124``````
``````// Include namespace system
using System;
// Csharp Program
// Remove all the Even digit sum nodes from a circular linked list
{
public int data;
{
this.data = data;
this.next = null;
}
}
{
{
// Set initial value
this.tail = null;
}
public void insert(int value)
{
{
}
else
{
this.tail.next = node;
}
this.tail = node;
}
// Display node element of circular linked list
public void display()
{
{
}
else
{
// First node of linked list
while (temp != null && temp != this.head)
{
// Display node value
Console.Write("  " + temp.data);
// visit to next node
temp = temp.next;
}
}
}
public int digitSum(int num)
{
int n = num;
int sum = 0;
if (n < 0)
{
n = -n;
}
while (n > 0)
{
sum += (n % 10);
n = n / 10;
}
return sum;
}
public void removeEvenDigitSumNode()
{
{
return;
}
while (temp != null)
{
if ((this.digitSum(temp.data) & 1) == 0)
{
auxiliary = temp;
temp = temp.next;
{
if (auxiliary == this.tail)
{
// When Remove last node
this.tail = null;
temp = null;
point = null;
}
else
{
this.tail.next = temp;
}
}
else if (auxiliary == this.tail)
{
// When Remove last node
if (point != null)
{
}
this.tail = point;
}
else
{
// When removing intermediate node
if (point != null)
{
point.next = temp;
}
}
auxiliary = null;
}
else
{
point = temp;
// visit to next node
temp = temp.next;
{
// Stop the process
temp = null;
}
}
}
}
public static void Main(String[] args)
{
// Insert node
cll.insert(13);
cll.insert(12);
cll.insert(32);
cll.insert(1);
cll.insert(14);
cll.insert(91);
cll.insert(27);
cll.insert(124);
cll.insert(88);
Console.WriteLine("\n Before remove even digit sum nodes");
cll.display();
/*
Node   Digit Sum    Even
----- --------    --------
13    [1+3] 4       Yes
12    [1+2] 3       No
32    [3+2] 5       No
1     [1]   1       No
14    [1+4] 5       No
91    [9+1] 10      Yes
27    [2+7] 9       No
124   [1+2+4] 7     No
88    [8+8] 16      Yes
--------------------------
Result
------
12 → 32 → 1 → 14 → 27 → 124 ┐
└-------------------------⤶
*/
cll.removeEvenDigitSumNode();
Console.WriteLine("\n After remove even digit sum nodes");
cll.display();
}
}``````

#### Output

`````` Before remove even digit sum nodes
13  12  32  1  14  91  27  124  88
After remove even digit sum nodes
12  32  1  14  27  124``````
``````package main
import "fmt"
// Go Program
// Remove all the Even digit sum nodes from a circular linked list
data int
}
me.data = data
me.next = nil
return me
}
}
// Set initial value
me.tail = nil
return me
}
} else {
this.tail.next = node
}
this.tail = node
}
// Display node element of circular linked list
} else {
// First node of linked list
for (temp != nil && temp != this.head) {
// Display node value
fmt.Print("  ", temp.data)
// visit to next node
temp = temp.next
}
}
}
func(this CircularLinkedList) digitSum(num int) int {
var n int = num
var sum int = 0
if n < 0 {
n = -n
}
for (n > 0) {
sum += (n % 10)
n = n / 10
}
return sum
}
return
}
var point * LinkNode = nil
for (temp != nil) {
if (this.digitSum(temp.data) & 1) == 0 {
auxiliary = temp
temp = temp.next
if auxiliary == this.tail {
// When Remove last node
this.tail = nil
temp = nil
point = nil
} else {
this.tail.next = temp
}
} else if auxiliary == this.tail {
// When Remove last node
if point != nil {
}
this.tail = point
} else {
// When removing intermediate node
if point != nil {
point.next = temp
}
}
auxiliary = nil
} else {
point = temp
// visit to next node
temp = temp.next
// Stop the process
temp = nil
}
}
}
}
func main() {
// Insert node
cll.insert(13)
cll.insert(12)
cll.insert(32)
cll.insert(1)
cll.insert(14)
cll.insert(91)
cll.insert(27)
cll.insert(124)
cll.insert(88)
fmt.Println("\n Before remove even digit sum nodes")
cll.display()
/*
Node   Digit Sum    Even
----- --------    --------
13    [1+3] 4       Yes
12    [1+2] 3       No
32    [3+2] 5       No
1     [1]   1       No
14    [1+4] 5       No
91    [9+1] 10      Yes
27    [2+7] 9       No
124   [1+2+4] 7     No
88    [8+8] 16      Yes
--------------------------
Result
------
12 → 32 → 1 → 14 → 27 → 124 ┐
└-------------------------⤶
*/
cll.removeEvenDigitSumNode()
fmt.Println("\n After remove even digit sum nodes")
cll.display()
}``````

#### Output

`````` Before remove even digit sum nodes
13  12  32  1  14  91  27  124  88
After remove even digit sum nodes
12  32  1  14  27  124``````
``````<?php
// Php Program
// Remove all the Even digit sum nodes from a circular linked list
{
public \$data;
public \$next;
public	function __construct(\$data)
{
\$this->data = \$data;
\$this->next = NULL;
}
}
{
public \$tail;
public	function __construct()
{
\$this->tail = NULL;
}
public	function insert(\$value)
{
{
}
else
{
\$this->tail->next = \$node;
}
\$this->tail = \$node;
}
// Display node element of circular linked list
public	function display()
{
{
}
else
{
// First node of linked list
while (\$temp != NULL && \$temp != \$this->head)
{
// Display node value
echo("  ".\$temp->data);
// visit to next node
\$temp = \$temp->next;
}
}
}
public	function digitSum(\$num)
{
\$n = \$num;
\$sum = 0;
if (\$n < 0)
{
\$n = -\$n;
}
while (\$n > 0)
{
\$sum += (\$n % 10);
\$n = (int)(\$n / 10);
}
return \$sum;
}
public	function removeEvenDigitSumNode()
{
{
return;
}
\$point = NULL;
while (\$temp != NULL)
{
if ((\$this->digitSum(\$temp->data) & 1) == 0)
{
\$auxiliary = \$temp;
\$temp = \$temp->next;
{
if (\$auxiliary == \$this->tail)
{
// When Remove last node
\$this->tail = NULL;
\$temp = NULL;
\$point = NULL;
}
else
{
\$this->tail->next = \$temp;
}
}
else if (\$auxiliary == \$this->tail)
{
// When Remove last node
if (\$point != NULL)
{
}
\$this->tail = \$point;
}
else
{
// When removing intermediate node
if (\$point != NULL)
{
\$point->next = \$temp;
}
}
\$auxiliary = NULL;
}
else
{
\$point = \$temp;
// visit to next node
\$temp = \$temp->next;
{
// Stop the process
\$temp = NULL;
}
}
}
}
}

function main()
{
// Insert node
\$cll->insert(13);
\$cll->insert(12);
\$cll->insert(32);
\$cll->insert(1);
\$cll->insert(14);
\$cll->insert(91);
\$cll->insert(27);
\$cll->insert(124);
\$cll->insert(88);
echo("\n Before remove even digit sum nodes".
"\n");
\$cll->display();
/*
Node   Digit Sum    Even
----- --------    --------
13    [1+3] 4       Yes
12    [1+2] 3       No
32    [3+2] 5       No
1     [1]   1       No
14    [1+4] 5       No
91    [9+1] 10      Yes
27    [2+7] 9       No
124   [1+2+4] 7     No
88    [8+8] 16      Yes
--------------------------
Result
------
12 → 32 → 1 → 14 → 27 → 124 ┐
└-------------------------⤶
*/
\$cll->removeEvenDigitSumNode();
echo("\n After remove even digit sum nodes".
"\n");
\$cll->display();
}
main();``````

#### Output

`````` Before remove even digit sum nodes
13  12  32  1  14  91  27  124  88
After remove even digit sum nodes
12  32  1  14  27  124``````
``````// Node JS Program
// Remove all the Even digit sum nodes from a circular linked list
{
constructor(data)
{
this.data = data;
this.next = null;
}
}
{
constructor()
{
this.tail = null;
}
insert(value)
{
{
}
else
{
this.tail.next = node;
}
this.tail = node;
}
// Display node element of circular linked list
display()
{
{
}
else
{
// First node of linked list
while (temp != null && temp != this.head)
{
// Display node value
process.stdout.write("  " + temp.data);
// visit to next node
temp = temp.next;
}
}
}
digitSum(num)
{
var n = num;
var sum = 0;
if (n < 0)
{
n = -n;
}
while (n > 0)
{
sum += (n % 10);
n = parseInt(n / 10);
}
return sum;
}
removeEvenDigitSumNode()
{
{
return;
}
var point = null;
while (temp != null)
{
if ((this.digitSum(temp.data) & 1) == 0)
{
auxiliary = temp;
temp = temp.next;
{
if (auxiliary == this.tail)
{
// When Remove last node
this.tail = null;
temp = null;
point = null;
}
else
{
this.tail.next = temp;
}
}
else if (auxiliary == this.tail)
{
// When Remove last node
if (point != null)
{
}
this.tail = point;
}
else
{
// When removing intermediate node
if (point != null)
{
point.next = temp;
}
}
auxiliary = null;
}
else
{
point = temp;
// visit to next node
temp = temp.next;
{
// Stop the process
temp = null;
}
}
}
}
}

function main()
{
// Insert node
cll.insert(13);
cll.insert(12);
cll.insert(32);
cll.insert(1);
cll.insert(14);
cll.insert(91);
cll.insert(27);
cll.insert(124);
cll.insert(88);
console.log("\n Before remove even digit sum nodes");
cll.display();
/*
Node   Digit Sum    Even
----- --------    --------
13    [1+3] 4       Yes
12    [1+2] 3       No
32    [3+2] 5       No
1     [1]   1       No
14    [1+4] 5       No
91    [9+1] 10      Yes
27    [2+7] 9       No
124   [1+2+4] 7     No
88    [8+8] 16      Yes
--------------------------
Result
------
12 → 32 → 1 → 14 → 27 → 124 ┐
└-------------------------⤶
*/
cll.removeEvenDigitSumNode();
console.log("\n After remove even digit sum nodes");
cll.display();
}
main();``````

#### Output

`````` Before remove even digit sum nodes
13  12  32  1  14  91  27  124  88
After remove even digit sum nodes
12  32  1  14  27  124``````
``````#  Python 3 Program
#  Remove all the Even digit sum nodes from a circular linked list
def __init__(self, data) :
self.data = data
self.next = None

def __init__(self) :
self.tail = None

def insert(self, value) :
else :
self.tail.next = node

self.tail = node

#  Display node element of circular linked list
def display(self) :
else :
#  First node of linked list
print(" ", self.head.data, end = "")
while (temp != None and temp != self.head) :
#  Display node value
print("  ", temp.data, end = "")
#  visit to next node
temp = temp.next

def digitSum(self, num) :
n = num
sum = 0
if (n < 0) :
n = -n

while (n > 0) :
sum += (n % 10)
n = int(n / 10)

return sum

def removeEvenDigitSumNode(self) :
return

point = None
while (temp != None) :
if ((self.digitSum(temp.data) & 1) == 0) :
auxiliary = temp
temp = temp.next
if (auxiliary == self.tail) :
#  When Remove last node
self.tail = None
temp = None
point = None
else :
self.tail.next = temp

elif (auxiliary == self.tail) :
#  When Remove last node
if (point != None) :

self.tail = point
else :
#  When removing intermediate node
if (point != None) :
point.next = temp

auxiliary = None
else :
point = temp
#  visit to next node
temp = temp.next
#  Stop the process
temp = None

def main() :
#  Insert node
cll.insert(13)
cll.insert(12)
cll.insert(32)
cll.insert(1)
cll.insert(14)
cll.insert(91)
cll.insert(27)
cll.insert(124)
cll.insert(88)
print("\n Before remove even digit sum nodes")
cll.display()
#    Node   Digit Sum    Even
#    ----- --------    --------
#    13    [1+3] 4       Yes
#    12    [1+2] 3       No
#    32    [3+2] 5       No
#    1     [1]   1       No
#    14    [1+4] 5       No
#    91    [9+1] 10      Yes
#    27    [2+7] 9       No
#    124   [1+2+4] 7     No
#    88    [8+8] 16      Yes
#    --------------------------
#    Result
#    ------
#    12 → 32 → 1 → 14 → 27 → 124 ┐
#     └-------------------------⤶
cll.removeEvenDigitSumNode()
print("\n After remove even digit sum nodes")
cll.display()

if __name__ == "__main__": main()``````

#### Output

`````` Before remove even digit sum nodes
13   12   32   1   14   91   27   124   88
After remove even digit sum nodes
12   32   1   14   27   124``````
``````#  Ruby Program
#  Remove all the Even digit sum nodes from a circular linked list
attr_accessor :data, :next
def initialize(data)
self.data = data
self.next = nil
end

end

def initialize()
self.tail = nil
end

def insert(value)
else

self.tail.next = node
end

self.tail = node
end

#  Display node element of circular linked list
def display()
else

#  First node of linked list
while (temp != nil && temp != self.head)
#  Display node value
print("  ", temp.data)
#  visit to next node
temp = temp.next
end

end

end

def digitSum(num)
n = num
sum = 0
if (n < 0)
n = -n
end

while (n > 0)
sum += (n % 10)
n = n / 10
end

return sum
end

def removeEvenDigitSumNode()
return
end

point = nil
while (temp != nil)
if ((self.digitSum(temp.data) & 1) == 0)
auxiliary = temp
temp = temp.next
if (auxiliary == self.tail)
#  When Remove last node
self.tail = nil
temp = nil
point = nil
else

self.tail.next = temp
end

elsif (auxiliary == self.tail)
#  When Remove last node
if (point != nil)
end

self.tail = point
else

#  When removing intermediate node
if (point != nil)
point.next = temp
end

end

auxiliary = nil
else

point = temp
#  visit to next node
temp = temp.next
#  Stop the process
temp = nil
end

end

end

end

end

def main()
#  Insert node
cll.insert(13)
cll.insert(12)
cll.insert(32)
cll.insert(1)
cll.insert(14)
cll.insert(91)
cll.insert(27)
cll.insert(124)
cll.insert(88)
print("\n Before remove even digit sum nodes", "\n")
cll.display()
#    Node   Digit Sum    Even
#    ----- --------    --------
#    13    [1+3] 4       Yes
#    12    [1+2] 3       No
#    32    [3+2] 5       No
#    1     [1]   1       No
#    14    [1+4] 5       No
#    91    [9+1] 10      Yes
#    27    [2+7] 9       No
#    124   [1+2+4] 7     No
#    88    [8+8] 16      Yes
#    --------------------------
#    Result
#    ------
#    12 → 32 → 1 → 14 → 27 → 124 ┐
#     └-------------------------⤶
cll.removeEvenDigitSumNode()
print("\n After remove even digit sum nodes", "\n")
cll.display()
end

main()``````

#### Output

`````` Before remove even digit sum nodes
13  12  32  1  14  91  27  124  88
After remove even digit sum nodes
12  32  1  14  27  124``````
``````// Scala Program
// Remove all the Even digit sum nodes from a circular linked list
{
def this(data: Int)
{
this(data, null);
}
}
{
def this()
{
this(null, null);
}
def insert(value: Int): Unit = {
{
}
else
{
this.tail.next = node;
}
this.tail = node;
}
// Display node element of circular linked list
def display(): Unit = {
{
}
else
{
// First node of linked list
while (temp != null && temp != this.head)
{
// Display node value
print("  " + temp.data);
// visit to next node
temp = temp.next;
}
}
}
def digitSum(num: Int): Int = {
var n: Int = num;
var sum: Int = 0;
if (n < 0)
{
n = -n;
}
while (n > 0)
{
sum += (n % 10);
n = n / 10;
}
return sum;
}
def removeEvenDigitSumNode(): Unit = {
{
return;
}
while (temp != null)
{
if ((digitSum(temp.data) & 1) == 0)
{
auxiliary = temp;
temp = temp.next;
{
if (auxiliary == this.tail)
{
// When Remove last node
this.tail = null;
temp = null;
point = null;
}
else
{
this.tail.next = temp;
}
}
else if (auxiliary == this.tail)
{
// When Remove last node
if (point != null)
{
}
this.tail = point;
}
else
{
// When removing intermediate node
if (point != null)
{
point.next = temp;
}
}
auxiliary = null;
}
else
{
point = temp;
// visit to next node
temp = temp.next;
{
// Stop the process
temp = null;
}
}
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
// Insert node
cll.insert(13);
cll.insert(12);
cll.insert(32);
cll.insert(1);
cll.insert(14);
cll.insert(91);
cll.insert(27);
cll.insert(124);
cll.insert(88);
println("\n Before remove even digit sum nodes");
cll.display();
/*
Node   Digit Sum    Even
----- --------    --------
13    [1+3] 4       Yes
12    [1+2] 3       No
32    [3+2] 5       No
1     [1]   1       No
14    [1+4] 5       No
91    [9+1] 10      Yes
27    [2+7] 9       No
124   [1+2+4] 7     No
88    [8+8] 16      Yes
--------------------------
Result
------
12 → 32 → 1 → 14 → 27 → 124 ┐
└-------------------------⤶
*/
cll.removeEvenDigitSumNode();
println("\n After remove even digit sum nodes");
cll.display();
}
}``````

#### Output

`````` Before remove even digit sum nodes
13  12  32  1  14  91  27  124  88
After remove even digit sum nodes
12  32  1  14  27  124``````
``````// Swift 4 Program
// Remove all the Even digit sum nodes from a circular linked list
{
var data: Int;
init(_ data: Int)
{
self.data = data;
self.next = nil;
}
}
{
init()
{
self.tail = nil;
}
func insert(_ value: Int)
{
{
}
else
{
self.tail!.next = node;
}
self.tail = node;
}
// Display node element of circular linked list
func display()
{
{
}
else
{
// First node of linked list
while (temp  != nil && !(temp === self.head))
{
// Display node value
print("  ", temp!.data, terminator: "");
// visit to next node
temp = temp!.next;
}
}
}
func digitSum(_ num: Int) -> Int
{
var n: Int = num;
var sum: Int = 0;
if (n < 0)
{
n = -n;
}
while (n > 0)
{
sum += (n % 10);
n = n / 10;
}
return sum;
}
func removeEvenDigitSumNode()
{
{
return;
}
while (temp  != nil)
{
if ((self.digitSum(temp!.data) & 1) == 0)
{
auxiliary = temp;
temp = temp!.next;
{
if (auxiliary === self.tail)
{
// When Remove last node
self.tail = nil;
temp = nil;
point = nil;
}
else
{
self.tail!.next = temp;
}
}
else if (auxiliary === self.tail)
{
// When Remove last node
if (point  != nil)
{
}
self.tail = point;
}
else
{
// When removing intermediate node
if (point  != nil)
{
point!.next = temp;
}
}
auxiliary = nil;
}
else
{
point = temp;
// visit to next node
temp = temp!.next;
{
// Stop the process
temp = nil;
}
}
}
}
}
func main()
{
// Insert node
cll.insert(13);
cll.insert(12);
cll.insert(32);
cll.insert(1);
cll.insert(14);
cll.insert(91);
cll.insert(27);
cll.insert(124);
cll.insert(88);
print("\n Before remove even digit sum nodes");
cll.display();
/*
Node   Digit Sum    Even
----- --------    --------
13    [1+3] 4       Yes
12    [1+2] 3       No
32    [3+2] 5       No
1     [1]   1       No
14    [1+4] 5       No
91    [9+1] 10      Yes
27    [2+7] 9       No
124   [1+2+4] 7     No
88    [8+8] 16      Yes
--------------------------
Result
------
12 → 32 → 1 → 14 → 27 → 124 ┐
└-------------------------⤶
*/
cll.removeEvenDigitSumNode();
print("\n After remove even digit sum nodes");
cll.display();
}
main();``````

#### Output

`````` Before remove even digit sum nodes
13   12   32   1   14   91   27   124   88
After remove even digit sum nodes
12   32   1   14   27   124``````
``````// Kotlin Program
// Remove all the Even digit sum nodes from a circular linked list
{
var data: Int;
constructor(data: Int)
{
this.data = data;
this.next = null;
}
}
{
constructor()
{
this.tail = null;
}
fun insert(value: Int): Unit
{
{
}
else
{
this.tail?.next = node;
}
this.tail = node;
}
// Display node element of circular linked list
fun display(): Unit
{
{
}
else
{
// First node of linked list
while (temp != null && temp != this.head)
{
// Display node value
print("  " + temp.data);
// visit to next node
temp = temp.next;
}
}
}
fun digitSum(num: Int): Int
{
var n: Int = num;
var sum: Int = 0;
if (n < 0)
{
n = -n;
}
while (n > 0)
{
sum += (n % 10);
n = n / 10;
}
return sum;
}
fun removeEvenDigitSumNode(): Unit
{
{
return;
}
var point: LinkNode ? = null;
while (temp != null)
{
if ((this.digitSum(temp.data) and 1) == 0)
{
auxiliary = temp;
temp = temp.next;
{
if (auxiliary == this.tail)
{
// When Remove last node
this.tail = null;
temp = null;
point = null;
}
else
{
this.tail?.next = temp;
}
}
else if (auxiliary == this.tail)
{
// When Remove last node
if (point != null)
{
}
this.tail = point;
}
else
{
// When removing intermediate node
if (point != null)
{
point.next = temp;
}
}

}
else
{
point = temp;
// visit to next node
temp = temp.next;
{
// Stop the process
temp = null;
}
}
}
}
}
fun main(args: Array < String > ): Unit
{
// Insert node
cll.insert(13);
cll.insert(12);
cll.insert(32);
cll.insert(1);
cll.insert(14);
cll.insert(91);
cll.insert(27);
cll.insert(124);
cll.insert(88);
println("\n Before remove even digit sum nodes");
cll.display();
/*
Node   Digit Sum    Even
----- --------    --------
13    [1+3] 4       Yes
12    [1+2] 3       No
32    [3+2] 5       No
1     [1]   1       No
14    [1+4] 5       No
91    [9+1] 10      Yes
27    [2+7] 9       No
124   [1+2+4] 7     No
88    [8+8] 16      Yes
--------------------------
Result
------
12 → 32 → 1 → 14 → 27 → 124 ┐
└-------------------------⤶
*/
cll.removeEvenDigitSumNode();
println("\n After remove even digit sum nodes");
cll.display();
}``````

#### Output

`````` Before remove even digit sum nodes
13  12  32  1  14  91  27  124  88
After remove even digit sum nodes
12  32  1  14  27  124``````

## Time Complexity

The time complexity of removing nodes with even digit sums from a circular linked list is O(n), where n is the number of nodes in the linked list. In the worst case, the algorithm may need to traverse all nodes in the circular linked list to determine whether the digit sum is even or odd.

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post