Given a singly Circular linked list. The task is to find the sum of nodes of the given linked list.
For the above circular list, sum = 2 + 5 + 7 + 8 + 10 = 32
Examples:
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
Approach:
- Initialize a pointer temp with the head of the linked list and a sum variable with 0.
- Start traversing the linked list using a loop until all the nodes get traversed.
- Add the value of current node to the sum i.e. sum += temp -> data.
- Increment the pointer to the next node of linked list i.e. temp = temp -> next.
- Return the sum.
Below is the implementation of the above approach:
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> |
Output
Sum of Circular linked list is = 81
complexity Analysis:
- Time Complexity: O(N) where N is the number of nodes in the linked list.
- Auxiliary Space: O(1)