# Sum of palindromic numbers in a linked list

Here given code implementation process.

``````// C Program
// Sum of palindromic numbers in a linked list
#include <stdio.h>
//For malloc function
#include <stdlib.h>

struct Node
{
int data;
struct Node *next;
};
//Create a node of linked list
struct Node *create_node(int data)
{
//Create dynamic node
struct Node *node = (struct Node *) malloc(sizeof(struct Node));
if (node == NULL)
{
printf("Memory overflow\n");
}
else
{
//Set initial node value
node->data = data;
node->next = NULL;
}
return node;
}
void insert(struct Node **head, int data)
{
struct Node *node = create_node(data);
{
}
else
{
//Find last node
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = node;
}
}
{
{
return;
}
while (temp != NULL)
{
printf("  %d", temp->data);
//visit to next node
temp = temp->next;
}
printf("\n");
}
//Check that whether given number is palindrome or not
int is_palindrome(int num)
{
if (num < 0)
{
//negative number is not a part of palindrome number
return 0;
}
int result = 0;
int x = num;
//Reversing the given number digit
while (x != 0)
{
result = (result *10) + (x % 10);
x = x / 10;
}
//true, when reverse number is equal to given num
return result == num;
}
//Calculate sum of all nodes that value is form of palindrome
{
//Define resultant variable
int result = 0;
//Get first node
printf(" Palindrome Nodes : [");
while (temp != NULL)
{
if (is_palindrome(temp->data) == 1)
{
printf("  %d", temp->data);
result += temp->data;
}
//visit to next node
temp = temp->next;
}
printf("  ]\n");
//Display the calculated result
printf(" Total Sum :  %d \n", result);
}
int main()
{
return 0;
}``````

#### Output

`````` Linked List :   21  111  212  11  112  7  -121  124  121
Palindrome Nodes : [  111  212  11  7  121  ]
Total Sum :  462``````
``````// Java Program
// Sum of palindromic numbers in a linked list

class Node
{
public int data;
public Node next;
public Node(int data)
{
//Set node value
this.data = data;
this.next = null;
}
}
{
public Node tail;
//Class constructors
{
this.tail = null;
}
//insert node at last of linke list
public void insert(int data)
{
//Create a node
Node node = new Node(data);
{
this.tail = node;
}
else
{
this.tail.next = node;
this.tail = node;
}
}
public void display()
{
{
return;
}
while (temp != null)
{
//display node value
System.out.print("  " + temp.data);
//visit to next node
temp = temp.next;
}
System.out.print("\n");
}
//Check that whether given number is palindrome or not
public boolean is_palindrome(int num)
{
if (num < 0)
{
return false;
}
//get given number
int x = num;
int result = 0;
//Reversing the given number digit
while (x != 0)
{
result = (result * 10) + (x % 10);
x = x / 10;
}
//true, when reverse number is equal to given num
return result == num;
}
//Calculate sum of all nodes that value is form of palindrome
public void sum_of_palindrome()
{
this.display();
//Define resultant variable
int result = 0;
//Get first node
System.out.print(" Palindrome Nodes : [");
while (temp != null)
{
if (is_palindrome(temp.data) == true)
{
System.out.print(" " + temp.data);
result += temp.data;
}
//visit to next node
temp = temp.next;
}
System.out.print(" ]\n");
//Display the calculated result
System.out.print(" Total Sum :  " + result + " \n");
}
public static void main(String[] args)
{
obj.insert(21);
obj.insert(111);
obj.insert(212);
obj.insert(11);
obj.insert(112);
obj.insert(7);
obj.insert(-121);
obj.insert(124);
obj.insert(121);
obj.sum_of_palindrome();
}
}``````

#### Output

`````` Linked List :   21  111  212  11  112  7  -121  124  121
Palindrome Nodes : [ 111 212 11 7 121 ]
Total Sum :  462``````
``````//Include header file
#include <iostream>

using namespace std;
// C++ Program
// Sum of palindromic numbers in a linked list

class Node
{
public: int data;
Node * next;
Node(int data)
{
//Set node value
this->data = data;
this->next = NULL;
}
};
{
Node * tail;
//Class constructors
{
this->tail = NULL;
}
//insert node at last of linke list
void insert(int data)
{
//Create a node
Node * node = new Node(data);
{
this->tail = node;
}
else
{
this->tail->next = node;
this->tail = node;
}
}
void display()
{
{
return;
}
cout << "\n Linked List : ";
while (temp != NULL)
{
//display node value
cout << "  " << temp->data;
//visit to next node
temp = temp->next;
}
cout << "\n";
}
//Check that whether given number is palindrome or not
bool is_palindrome(int num)
{
if (num < 0)
{
return false;
}
//get given number
int x = num;
int result = 0;
//Reversing the given number digit
while (x != 0)
{
result = (result * 10) + (x % 10);
x = x / 10;
}
//true, when reverse number is equal to given num
return result == num;
}
//Calculate sum of all nodes that value is form of palindrome
void sum_of_palindrome()
{
this->display();
//Define resultant variable
int result = 0;
//Get first node
cout << " Palindrome Nodes : [";
while (temp != NULL)
{
if (this->is_palindrome(temp->data) == true)
{
cout << " " << temp->data;
result += temp->data;
}
//visit to next node
temp = temp->next;
}
cout << " ]\n";
//Display the calculated result
cout << " Total Sum :  " << result << " \n";
}
};
int main()
{
obj.insert(21);
obj.insert(111);
obj.insert(212);
obj.insert(11);
obj.insert(112);
obj.insert(7);
obj.insert(-121);
obj.insert(124);
obj.insert(121);
obj.sum_of_palindrome();
return 0;
}``````

#### Output

`````` Linked List :   21  111  212  11  112  7  -121  124  121
Palindrome Nodes : [ 111 212 11 7 121 ]
Total Sum :  462``````
``````//Include namespace system
using System;
// C# Program
// Sum of palindromic numbers in a linked list

class Node
{
public int data;
public Node next;
public Node(int data)
{
//Set node value
this.data = data;
this.next = null;
}
}
{
public Node tail;
//Class constructors
{
this.tail = null;
}
//insert node at last of linke list
public void insert(int data)
{
//Create a node
Node node = new Node(data);
{
this.tail = node;
}
else
{
this.tail.next = node;
this.tail = node;
}
}
public void display()
{
{
return;
}
while (temp != null)
{
//display node value
Console.Write("  " + temp.data);
//visit to next node
temp = temp.next;
}
Console.Write("\n");
}
//Check that whether given number is palindrome or not
public Boolean is_palindrome(int num)
{
if (num < 0)
{
return false;
}
//get given number
int x = num;
int result = 0;
//Reversing the given number digit
while (x != 0)
{
result = (result * 10) + (x % 10);
x = x / 10;
}
//true, when reverse number is equal to given num
return result == num;
}
//Calculate sum of all nodes that value is form of palindrome
public void sum_of_palindrome()
{
this.display();
//Define resultant variable
int result = 0;
//Get first node
Console.Write(" Palindrome Nodes : [");
while (temp != null)
{
if (is_palindrome(temp.data) == true)
{
Console.Write(" " + temp.data);
result += temp.data;
}
//visit to next node
temp = temp.next;
}
Console.Write(" ]\n");
//Display the calculated result
Console.Write(" Total Sum :  " + result + " \n");
}
public static void Main(String[] args)
{
obj.insert(21);
obj.insert(111);
obj.insert(212);
obj.insert(11);
obj.insert(112);
obj.insert(7);
obj.insert(-121);
obj.insert(124);
obj.insert(121);
obj.sum_of_palindrome();
}
}``````

#### Output

`````` Linked List :   21  111  212  11  112  7  -121  124  121
Palindrome Nodes : [ 111 212 11 7 121 ]
Total Sum :  462``````
``````<?php
// Php Program
// Sum of palindromic numbers in a linked list

class Node
{
public \$data;
public \$next;

function __construct(\$data)
{
//Set node value
\$this->data = \$data;
\$this->next = null;
}
}
{
public \$tail;
//Class constructors
function __construct()
{
\$this->tail = null;
}
//insert node at last of linke list
public	function insert(\$data)
{
//Create a node
\$node = new Node(\$data);
{
\$this->tail = \$node;
}
else
{
\$this->tail->next = \$node;
\$this->tail = \$node;
}
}
public	function display()
{
{
return;
}
echo "\n Linked List : ";
while (\$temp != null)
{
//display node value
echo "  ". \$temp->data;
//visit to next node
\$temp = \$temp->next;
}
echo "\n";
}
//Check that whether given number is palindrome or not
public	function is_palindrome(\$num)
{
if (\$num < 0)
{
return false;
}
//get given number
\$x = \$num;
\$result = 0;
//Reversing the given number digit
while (\$x != 0)
{
\$result = (\$result * 10) + (\$x % 10);
\$x = intval(\$x / 10);
}
//true, when reverse number is equal to given num
return \$result == \$num;
}
//Calculate sum of all nodes that value is form of palindrome
public	function sum_of_palindrome()
{
\$this->display();
//Define resultant variable
\$result = 0;
//Get first node
echo " Palindrome Nodes : [";
while (\$temp != null)
{
if (\$this->is_palindrome(\$temp->data) == true)
{
echo " ". \$temp->data;
\$result += \$temp->data;
}
//visit to next node
\$temp = \$temp->next;
}
echo " ]\n";
//Display the calculated result
echo " Total Sum :  ". \$result ." \n";
}
}

function main()
{
\$obj->insert(21);
\$obj->insert(111);
\$obj->insert(212);
\$obj->insert(11);
\$obj->insert(112);
\$obj->insert(7);
\$obj->insert(-121);
\$obj->insert(124);
\$obj->insert(121);
\$obj->sum_of_palindrome();
}
main();``````

#### Output

`````` Linked List :   21  111  212  11  112  7  -121  124  121
Palindrome Nodes : [ 111 212 11 7 121 ]
Total Sum :  462``````
``````// Node Js Program
// Sum of palindromic numbers in a linked list

class Node
{
constructor(data)
{
//Set node value
this.data = data;
this.next = null;
}
}
{
//Class constructors
constructor()
{
this.tail = null;
}
//insert node at last of linke list
insert(data)
{
//Create a node
var node = new Node(data);
{
this.tail = node;
}
else
{
this.tail.next = node;
this.tail = node;
}
}
display()
{
{
return;
}
while (temp != null)
{
//display node value
process.stdout.write("  " + temp.data);
//visit to next node
temp = temp.next;
}
process.stdout.write("\n");
}
//Check that whether given number is palindrome or not
is_palindrome(num)
{
if (num < 0)
{
return false;
}
//get given number
var x = num;
var result = 0;
//Reversing the given number digit
while (x != 0)
{
result = (result * 10) + (x % 10);
x = parseInt(x / 10);
}
//true, when reverse number is equal to given num
return result == num;
}
//Calculate sum of all nodes that value is form of palindrome
sum_of_palindrome()
{
this.display();
//Define resultant variable
var result = 0;
//Get first node
process.stdout.write(" Palindrome Nodes : [");
while (temp != null)
{
if (this.is_palindrome(temp.data) == true)
{
process.stdout.write(" " + temp.data);
result += temp.data;
}
//visit to next node
temp = temp.next;
}
process.stdout.write(" ]\n");
//Display the calculated result
process.stdout.write(" Total Sum :  " + result + " \n");
}
}

function main()
{
obj.insert(21);
obj.insert(111);
obj.insert(212);
obj.insert(11);
obj.insert(112);
obj.insert(7);
obj.insert(-121);
obj.insert(124);
obj.insert(121);
obj.sum_of_palindrome();
}
main();``````

#### Output

`````` Linked List :   21  111  212  11  112  7  -121  124  121
Palindrome Nodes : [ 111 212 11 7 121 ]
Total Sum :  462``````
``````#  Python 3 Program
#  Sum of palindromic numbers in a linked list

class Node :

def __init__(self, data) :
# Set node value
self.data = data
self.next = None

# Class constructors
def __init__(self) :
self.tail = None

# insert node at last of linke list
def insert(self, data) :
# Create a node
node = Node(data)
self.tail = node
else :
self.tail.next = node
self.tail = node

def display(self) :
print("\nEmpty linked list\n", end = "")
return

print("\n Linked List : ", end = "")
while (temp != None) :
# display node value
print("  ", temp.data, end = "")
# visit to next node
temp = temp.next

print(end = "\n")

# Check that whether given number is palindrome or not
def is_palindrome(self, num) :
if (num < 0) :
return False

# get given number
x = num
result = 0
# Reversing the given number digit
while (x != 0) :
result = (result * 10) + (x % 10)
x = int(x / 10)

# true, when reverse number is equal to given num
return result == num

# Calculate sum of all nodes that value is form of palindrome
def sum_of_palindrome(self) :
# Display element of linked list
self.display()
# Define resultant variable
result = 0
# Get first node
print(" Palindrome Nodes : [", end = "")
while (temp != None) :
if (self.is_palindrome(temp.data) == True) :
print(" ", temp.data, end = "")
result += temp.data

# visit to next node
temp = temp.next

print(" ]")
# Display the calculated result
print(" Total Sum :  ", result )

def main() :
obj.insert(21)
obj.insert(111)
obj.insert(212)
obj.insert(11)
obj.insert(112)
obj.insert(7)
obj.insert(-121)
obj.insert(124)
obj.insert(121)
obj.sum_of_palindrome()

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

#### Output

`````` Linked List :    21   111   212   11   112   7   -121   124   121
Palindrome Nodes : [  111  212  11  7  121 ]
Total Sum :   462``````
``````#  Ruby Program
#  Sum of palindromic numbers in a linked list

class Node

# Define the accessor and reader of class Node
attr_accessor :data, :next

def initialize(data)

# Set node value
self.data = data
self.next = nil
end
end

# Class constructors
def initialize()

self.tail = nil
end
# insert node at last of linke list
def insert(data)

# Create a node
node = Node.new(data)

self.tail = node
else

self.tail.next = node
self.tail = node
end
end
def display()

return
end
while (temp != nil)

# display node value
print("  ", temp.data)
# visit to next node
temp = temp.next
end
print("\n")
end
# Check that whether given number is palindrome or not
def is_palindrome(num)

if (num < 0)

return false
end
# get given number
x = num
result = 0
# Reversing the given number digit
while (x != 0)

result = (result * 10) + (x % 10)
x = x / 10
end
# true, when reverse number is equal to given num
return result == num
end
# Calculate sum of all nodes that value is form of palindrome
def sum_of_palindrome()

# Display element of linked list
self.display()
# Define resultant variable
result = 0
# Get first node
print(" Palindrome Nodes : [")
while (temp != nil)

if (self.is_palindrome(temp.data) == true)

print(" ", temp.data)
result += temp.data
end
# visit to next node
temp = temp.next
end
print(" ]\n")
# Display the calculated result
print(" Total Sum :  ", result ," \n")
end
end
def main()

obj.insert(21)
obj.insert(111)
obj.insert(212)
obj.insert(11)
obj.insert(112)
obj.insert(7)
obj.insert(-121)
obj.insert(124)
obj.insert(121)
obj.sum_of_palindrome()
end
main()``````

#### Output

`````` Linked List :   21  111  212  11  112  7  -121  124  121
Palindrome Nodes : [ 111 212 11 7 121 ]
Total Sum :  462
``````
``````// Scala Program
// Sum of palindromic numbers in a linked list

class Node(var data: Int,
var next: Node)
{
def this(data: Int)
{
this(data, null);
}
}
var tail: Node)
{
//Class constructors
def this()
{
this(null, null);
}
//insert node at last of linke list
def insert(data: Int): Unit = {
//Create a node
var node: Node = new Node(data);
{
this.tail = node;
}
else
{
this.tail.next = node;
this.tail = node;
}
}
def display(): Unit = {
{
return;
}
while (temp != null)
{
//display node value
print("  " + temp.data);
//visit to next node
temp = temp.next;
}
print("\n");
}
//Check that whether given number is palindrome or not
def is_palindrome(num: Int): Boolean = {
if (num < 0)
{
return false;
}
//get given number
var x: Int = num;
var result: Int = 0;
//Reversing the given number digit
while (x != 0)
{
result = (result * 10) + (x % 10);
x = (x / 10).toInt;
}
//true, when reverse number is equal to given num
return result == num;
}
//Calculate sum of all nodes that value is form of palindrome
def sum_of_palindrome(): Unit = {
this.display();
//Define resultant variable
var result: Int = 0;
//Get first node
print(" Palindrome Nodes : [");
while (temp != null)
{
if (is_palindrome(temp.data) == true)
{
print(" " + temp.data);
result += temp.data;
}
//visit to next node
temp = temp.next;
}
print(" ]\n");
//Display the calculated result
print(" Total Sum :  " + result + " \n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
obj.insert(21);
obj.insert(111);
obj.insert(212);
obj.insert(11);
obj.insert(112);
obj.insert(7);
obj.insert(-121);
obj.insert(124);
obj.insert(121);
obj.sum_of_palindrome();
}
}``````

#### Output

`````` Linked List :   21  111  212  11  112  7  -121  124  121
Palindrome Nodes : [ 111 212 11 7 121 ]
Total Sum :  462``````
``````// Swift Program
// Sum of palindromic numbers in a linked list

class Node
{
var data: Int;
var next: Node? ;
init(_ data: Int)
{
//Set node value
self.data = data;
self.next = nil;
}
}
{
var tail: Node? ;
//Class constructors
init()
{
self.tail = nil;
}
//insert node at last of linke list
func insert(_ data: Int)
{
//Create a node
let node: Node? = Node(data);
{
self.tail = node;
}
else
{
self.tail!.next = node;
self.tail = node;
}
}
func display()
{
{
return;
}
print("\n Linked List : ", terminator: "");
while (temp != nil)
{
//display node value
print("  ", temp!.data, terminator: "");
//visit to next node
temp = temp!.next;
}
print("\n", terminator: "");
}
//Check that whether given number is palindrome or not
func is_palindrome(_ num: Int) -> Bool
{
if (num < 0)
{
return false;
}
//get given number
var x: Int = num;
var result: Int = 0;
//Reversing the given number digit
while (x != 0)
{
result = (result * 10) + (x % 10);
x = x / 10;
}
//true, when reverse number is equal to given num
return result == num;
}
//Calculate sum of all nodes that value is form of palindrome
func sum_of_palindrome()
{
self.display();
//Define resultant variable
var result: Int = 0;
//Get first node
print(" Palindrome Nodes : [", terminator: "");
while (temp != nil)
{
if (self.is_palindrome(temp!.data) == true)
{
print(" ", temp!.data, terminator: "");
result += temp!.data;
}
//visit to next node
temp = temp!.next;
}
print(" ]\n", terminator: "");
//Display the calculated result
print(" Total Sum :  ", result ," \n", terminator: "");
}
}
func main()
{
obj.insert(21);
obj.insert(111);
obj.insert(212);
obj.insert(11);
obj.insert(112);
obj.insert(7);
obj.insert(-121);
obj.insert(124);
obj.insert(121);
obj.sum_of_palindrome();
}
main();``````

#### Output

`````` Linked List :    21   111   212   11   112   7   -121   124   121
Palindrome Nodes : [  111  212  11  7  121 ]
Total Sum :   462``````

## 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.