Sum of all Palindrome Numbers present in a Linked list
Last Updated :
11 Jan, 2023
Given a linked list with integer node values, the task is to find the sum of all Palindrome Numbers present as Node values.
Examples:
Input: 13 -> 212 -> 22 -> 44 -> 4 -> 3
Output: 285
Explanation: The sum of palindrome numbers {22, 212, 44, 4, 3} is 285
Input: 19 -> 22 -> 141
Output: 163
Approach: In order to solve this problem we are using an approach similar to calculate the Sum of all Palindromes in an Array. Iterate through all the nodes of the linked list and check if the current node value is a palindrome or not. If so, add the value and store the sum. The final sum of all such values gives the desired answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node* next;
};
void push(Node** head_ref, int new_data)
{
Node* new_node = (Node*) malloc (
sizeof ( struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
bool isPalin( int n)
{
int d = 0, s = 0;
int temp = n;
while (n > 0) {
d = n % 10;
s = s * 10 + d;
n = n / 10;
}
return temp == s;
}
int sumOfpal(Node* head_1)
{
int s = 0;
Node* ptr = head_1;
while (ptr != NULL) {
if (isPalin(ptr->data)) {
s += ptr->data;
}
ptr = ptr->next;
}
return s;
}
int main()
{
Node* head1 = NULL;
push(&head1, 13);
push(&head1, 212);
push(&head1, 22);
push(&head1, 44);
push(&head1, 4);
push(&head1, 3);
cout << sumOfpal(head1)
<< endl;
return 0;
}
|
Java
import java.util.*;
class GFG {
static class Node {
int data;
Node next;
};
static Node push(Node head_ref,
int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = (head_ref);
(head_ref) = new_node;
return head_ref;
}
static boolean isPalin( int n)
{
int d = 0 , s = 0 ;
int temp = n;
while (n > 0 ) {
d = n % 10 ;
s = s * 10 + d;
n = n / 10 ;
}
return temp == s;
}
static int sumOfpal(Node head_1)
{
int s = 0 ;
Node ptr = head_1;
while (ptr != null ) {
if (isPalin(ptr.data)) {
s += ptr.data;
}
ptr = ptr.next;
}
return s;
}
public static void main(String args[])
{
Node head1 = null ;
head1 = push(head1, 13 );
head1 = push(head1, 212 );
head1 = push(head1, 22 );
head1 = push(head1, 44 );
head1 = push(head1, 4 );
head1 = push(head1, 3 );
System.out.println(
sumOfpal(head1));
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = next
def push( head_ref, new_data) :
new_node = Node( 0 )
new_node.data = new_data
new_node. next = (head_ref)
(head_ref) = new_node
return head_ref
def isPalin(n) :
d = 0 ; s = 0 ;
temp = n;
while (n > 0 ) :
d = n % 10 ;
s = s * 10 + d;
n = n / / 10 ;
return s = = temp;
def sumOfpal(head_ref1) :
s = 0 ;
ptr1 = head_ref1
while (ptr1 ! = None ) :
if (isPalin(ptr1.data)) :
s = s + ptr1.data
ptr1 = ptr1. next
return s;
head1 = None
head1 = push(head1, 13 )
head1 = push(head1, 212 )
head1 = push(head1, 22 )
head1 = push(head1, 44 )
head1 = push(head1, 4 )
head1 = push(head1, 3 )
print (sumOfpal(head1))
|
C#
using System;
class GFG {
class Node
{
public int data;
public Node next;
};
static Node push(Node head_ref,
int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = (head_ref);
(head_ref) = new_node;
return head_ref;
}
static bool isPalin( int n)
{
int d = 0, s = 0;
int temp = n;
while (n > 0)
{
d = n % 10;
s = s * 10 + d;
n = n / 10;
}
return temp == s;
}
static int sumOfpal(Node head_1)
{
int s = 0;
Node ptr = head_1;
while (ptr != null )
{
if (isPalin(ptr.data))
{
s += ptr.data;
}
ptr = ptr.next;
}
return s;
}
public static void Main(String []args)
{
Node head1 = null ;
head1 = push(head1, 13);
head1 = push(head1, 212);
head1 = push(head1, 22);
head1 = push(head1, 44);
head1 = push(head1, 4);
head1 = push(head1, 3);
Console.WriteLine(sumOfpal(head1));
}
}
|
Javascript
<script>
class Node {
constructor() {
this .data = 0;
this .next = null ;
}
}
function push(head_ref, new_data) {
var new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
function isPalin(n) {
var d = 0,
s = 0;
var temp = n;
while (n > 0) {
d = n % 10;
s = s * 10 + d;
n = parseInt(n / 10);
}
return temp == s;
}
function sumOfpal(head_1) {
var s = 0;
var ptr = head_1;
while (ptr != null ) {
if (isPalin(ptr.data)) {
s += ptr.data;
}
ptr = ptr.next;
}
return s;
}
var head1 = null ;
head1 = push(head1, 13);
head1 = push(head1, 212);
head1 = push(head1, 22);
head1 = push(head1, 44);
head1 = push(head1, 4);
head1 = push(head1, 3);
document.write(sumOfpal(head1));
</script>
|
Share your thoughts in the comments
Please Login to comment...