Dada una lista enlazada con valores de Node enteros, la tarea es encontrar la suma de todos los números palíndromos presentes como valores de Node.
Ejemplos:
Entrada: 13 -> 212 -> 22 -> 44 -> 4 -> 3
Salida: 285
Explicación: La suma de los números palíndromos {22, 212, 44, 4, 3} es 285
Entrada: 19 -> 22 -> 141
Salida: 163
Enfoque: para resolver este problema, estamos utilizando un enfoque similar para calcular la Suma de todos los palíndromos en una array . Recorra todos los Nodes de la lista enlazada y verifique si el valor del Node actual es un palíndromo o no. Si es así, agregue el valor y almacene la suma. La suma final de todos estos valores da la respuesta deseada.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to calculate // the sum of all palindromic // numbers in a linked list #include <bits/stdc++.h> using namespace std; // Node of the singly // linked list struct Node { int data; Node* next; }; // Function to insert a node at // the beginning of the singly // Linked List void push(Node** head_ref, int new_data) { // allocate node Node* new_node = (Node*)malloc( sizeof(struct Node)); // Insert the data new_node->data = new_data; // Point the new Node // to the current head new_node->next = (*head_ref); // Make the new Node as // the new head (*head_ref) = new_node; } // Function to check // if a number n is // palindrome or not 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; } // If n is equal to // the its reverse // it is a palindrome return temp == s; } // Function to calculate // the sum of all nodes // which are palindrome int sumOfpal(Node* head_1) { int s = 0; Node* ptr = head_1; while (ptr != NULL) { // If the value of the // current node is // a palindrome if (isPalin(ptr->data)) { // Add the value // to the sum s += ptr->data; } ptr = ptr->next; } return s; } // Driver Code int main() { // Create the head // of the linked list Node* head1 = NULL; // Insert nodes into // the linked list push(&head1, 13); push(&head1, 212); push(&head1, 22); push(&head1, 44); push(&head1, 4); push(&head1, 3); // Print the sum of all // palindromes cout << sumOfpal(head1) << endl; return 0; }
Java
// Java program to calculate // the sum of all palindromes // in a linked list import java.util.*; class GFG { // Node of the singly // linked list static class Node { int data; Node next; }; // Function to insert a node // at the beginning of the // singly Linked List static Node push(Node head_ref, int new_data) { // Allocate node Node new_node = new Node(); // Insert the data new_node.data = new_data; // Point the current Node // to the current head new_node.next = (head_ref); // Make the current node // as the new head (head_ref) = new_node; return head_ref; } // Function to check if // a number is palindrome 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; } // If n is equal to its // reverse, it is a // palindrome return temp == s; } // Function to calculate sum // of all nodes with value // which is a palindrome static int sumOfpal(Node head_1) { int s = 0; Node ptr = head_1; while (ptr != null) { // If the value of the // current node // is a palindrome if (isPalin(ptr.data)) { // Add that value to // the sum s += ptr.data; } ptr = ptr.next; } // Return the sum return s; } // Driver Code public static void main(String args[]) { // Create the head Node head1 = null; // Insert nodes to the // Linked List 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
# Python3 program to # calculate the sum of all # palindromes present in # a Linked List class Node: def __init__(self, data): self.data = data self.next = next # Function to insert a # node at the beginning # of the singly Linked List def push( head_ref, new_data) : # Allocate node new_node = Node(0) # Insert data new_node.data = new_data # Pont to the head new_node.next = (head_ref) # Make the new Node # the new head (head_ref) = new_node return head_ref # Function to check if # a number is palindrome def isPalin(n) : d = 0; s = 0; temp = n; while (n > 0) : d = n % 10; s = s * 10 + d; n = n // 10; # If n is equal to its reverse, # it is a palindrome return s == temp; # Function to calculate sum of # all elements in a Linked List # which are palindrome def sumOfpal(head_ref1) : s = 0; ptr1 = head_ref1 while (ptr1 != None) : # If the value of the # current node # is a palindrome if (isPalin(ptr1.data)) : # Add that value s = s + ptr1.data # Move to the next node ptr1 = ptr1.next # Return the final sum return s; # Driver code # Create the Head head1 = None # Insert nodes 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#
// C# program to calculate // the sum of all palindromes // in a linked list using System; class GFG { // Node of the singly // linked list class Node { public int data; public Node next; }; // Function to insert a node // at the beginning of the // singly Linked List static Node push(Node head_ref, int new_data) { // Allocate node Node new_node = new Node(); // Insert the data new_node.data = new_data; // Point the current Node // to the current head new_node.next = (head_ref); // Make the current node // as the new head (head_ref) = new_node; return head_ref; } // Function to check if // a number is palindrome 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; } // If n is equal to its // reverse, it is a // palindrome return temp == s; } // Function to calculate sum // of all nodes with value // which is a palindrome static int sumOfpal(Node head_1) { int s = 0; Node ptr = head_1; while (ptr != null) { // If the value of the // current node // is a palindrome if (isPalin(ptr.data)) { // Add that value to // the sum s += ptr.data; } ptr = ptr.next; } // Return the sum return s; } // Driver Code public static void Main(String []args) { // Create the head Node head1 = null; // Insert nodes to the // Linked List 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)); } } // This code is contributed by sapnasingh4991
Javascript
<script> // JavaScript program to calculate // the sum of all palindromes // in a linked list // Node of the singly // linked list class Node { constructor() { this.data = 0; this.next = null; } } // Function to insert a node // at the beginning of the // singly Linked List function push(head_ref, new_data) { // Allocate node var new_node = new Node(); // Insert the data new_node.data = new_data; // Point the current Node // to the current head new_node.next = head_ref; // Make the current node // as the new head head_ref = new_node; return head_ref; } // Function to check if // a number is palindrome 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); } // If n is equal to its // reverse, it is a // palindrome return temp == s; } // Function to calculate sum // of all nodes with value // which is a palindrome function sumOfpal(head_1) { var s = 0; var ptr = head_1; while (ptr != null) { // If the value of the // current node // is a palindrome if (isPalin(ptr.data)) { // Add that value to // the sum s += ptr.data; } ptr = ptr.next; } // Return the sum return s; } // Driver Code // Create the head var head1 = null; // Insert nodes to the // Linked List 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)); // This code is contributed by rdtank. </script>
285
Publicación traducida automáticamente
Artículo escrito por ShivaTeja2 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA