Dada una lista enlazada circular única . La tarea es encontrar la suma de los Nodes de la lista enlazada dada.
Para la lista circular anterior, suma = 2 + 5 + 7 + 8 + 10 = 32
Ejemplos:
Input: 11->2->56->12 Output: Sum of Circular linked list is = 81 Input: 2-> 5 -> 7 -> 8 -> 10 Output: Sum of Circular linked list is = 32
Acercarse:
- Inicialice un puntero temporal con el encabezado de la lista vinculada y una variable de suma con 0.
- Comience a recorrer la lista vinculada utilizando un bucle hasta que se atraviesen todos los Nodes.
- Agregue el valor del Node actual a la suma, es decir, sum += temp -> data.
- Incremente el puntero al siguiente Node de la lista enlazada, es decir, temp = temp -> next.
- Devolver la suma.
A continuación se muestra la implementación del enfoque anterior:
C++
// CPP program to find the sum of all nodes // of a Circular linked list #include <bits/stdc++.h> using namespace std; // Structure for a node struct Node { int data; struct Node* next; }; // Function to insert a node at the beginning // of a Circular linked list void push(struct Node** head_ref, int data) { 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) { while (temp->next != *head_ref) temp = temp->next; temp->next = ptr1; } else ptr1->next = ptr1; // For the first node *head_ref = ptr1; } // Function to find sum of the given // Circular linked list int sumOfList(struct Node* head) { struct Node* temp = head; int sum = 0; if (head != NULL) { do { temp = temp->next; sum += temp->data; } while (temp != head); } return sum; } // Driver code int main() { // Initialize lists as empty struct Node* head = NULL; // Created linked list will be 11->2->56->12 push(&head, 12); push(&head, 56); push(&head, 2); push(&head, 11); cout << "Sum of Circular linked list is = " << sumOfList(head); return 0; }
Java
// Java program to find the sum of // all nodes of a Circular linked list import java.util.*; class GFG { // structure for a node static class Node { int data; Node next; }; // Function to insert a node // at the beginning of a // Circular linked list static Node push(Node head_ref, int data) { 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) { while (temp.next != head_ref) temp = temp.next; temp.next = ptr1; } else ptr1.next = ptr1; // For the first node head_ref = ptr1; return head_ref; } // Function to find sum of the // given Circular linked list static int sumOfList(Node head) { Node temp = head; int sum = 0; if (head != null) { do { temp = temp.next; sum += temp.data; } while (temp != head); } return sum; } // Driver code public static void main(String args[]) { // Initialize lists as empty Node head = null; // Created linked list will // be 11.2.56.12 head = push(head, 12); head = push(head, 56); head = push(head, 2); head = push(head, 11); System.out.println("Sum of Circular linked" + " list is = " + sumOfList(head)); } } // This code is contributed by Arnab Kundu
Python3
# Python3 program to find the sum of all nodes # of a Circular linked list import math # class for a node class Node: def __init__(self,data): self.data = data self.next = None # Function to insert a node at the beginning # of a Circular linked list def push(head, data): if not head: head = Node(data) head.next = head return head lnode = head # If linked list is not NULL then # set the next of last node while(lnode and lnode.next is not head): lnode = lnode.next ptr1 = Node(data) # For the first node ptr1.next = head lnode.next = ptr1 head = ptr1 return head # Function to find sum of the given # Circular linked list def sumOfList(head): temp = head tsum = temp.data temp = temp.next while(temp is not head): tsum += temp.data temp = temp.next return tsum # Driver code if __name__=='__main__': # Initialize lists as empty head = None # Created linked list will be 11->2->56->12 head = push(head, 12) head = push(head, 56) head = push(head, 2) head = push(head, 11) print("Sum of circular list is = {}" . format(sumOfList(head))) # This code is contributed by Vikash Kumar 37
C#
// C# program to find the sum of // all nodes of a Circular linked list using System; class GFG { // structure for a node class Node { public int data; public Node next; }; // Function to insert a node // at the beginning of a // Circular linked list static Node push(Node head_ref, int data) { 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) { while (temp.next != head_ref) temp = temp.next; temp.next = ptr1; } else ptr1.next = ptr1; // For the first node head_ref = ptr1; return head_ref; } // Function to find sum of the // given Circular linked list static int sumOfList(Node head) { Node temp = head; int sum = 0; if (head != null) { do { temp = temp.next; sum += temp.data; } while (temp != head); } return sum; } // Driver code public static void Main() { // Initialize lists as empty Node head = null; // Created linked list will // be 11.2.56.12 head = push(head, 12); head = push(head, 56); head = push(head, 2); head = push(head, 11); Console.WriteLine("Sum of Circular linked" + " list is = " + sumOfList(head)); } } // This code is contributed by princiraj1992
Javascript
<script> // JavaScript program to find the sum of // all nodes of a Circular linked list // structure for a node class 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) { 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) { while (temp.next != head_ref) temp = temp.next; temp.next = ptr1; } else ptr1.next = ptr1; // For the first node head_ref = ptr1; return head_ref; } // Function to find sum of the // given Circular linked list function sumOfList(head) { var temp = head; var sum = 0; if (head != null) { do { temp = temp.next; sum += temp.data; } while (temp != head); } return sum; } // Driver code // Initialize lists as empty var head = null; // Created linked list will // be 11.2.56.12 head = push(head, 12); head = push(head, 56); head = push(head, 2); head = push(head, 11); document.write("Sum of Circular linked" + " list is = " + sumOfList(head)); // This code contributed by aashish1995 </script>
Producción:
Sum of Circular linked list is = 81
Complejidad de tiempo : O(N) donde N es el número de Nodes en la lista enlazada.
Espacio Auxiliar: O(1)