Sum of the nodes of a Circular Linked List
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++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};
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 (*head_ref != NULL) {
while (temp->next != *head_ref)
temp = temp->next;
temp->next = ptr1;
}
else
ptr1->next = ptr1;
*head_ref = ptr1;
}
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;
}
int main()
{
struct Node* head = NULL;
push(&head, 12);
push(&head, 56);
push(&head, 2);
push(&head, 11);
cout << "Sum of Circular linked list is = " << sumOfList(head);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static class Node
{
int data;
Node next;
};
static Node push(Node head_ref,
int data)
{
Node ptr1 = new Node();
Node temp = head_ref;
ptr1.data = data;
ptr1.next = head_ref;
if (head_ref != null )
{
while (temp.next != head_ref)
temp = temp.next;
temp.next = ptr1;
}
else
ptr1.next = ptr1;
head_ref = ptr1;
return head_ref;
}
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;
}
public static void main(String args[])
{
Node head = null ;
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));
}
}
|
Python3
import math
class Node:
def __init__( self ,data):
self .data = data
self . next = None
def push(head, data):
if not head:
head = Node(data)
head. next = head
return head
lnode = head
while (lnode and lnode. next is not head):
lnode = lnode. next
ptr1 = Node(data)
ptr1. next = head
lnode. next = ptr1
head = ptr1
return head
def sumOfList(head):
temp = head
tsum = temp.data
temp = temp. next
while (temp is not head):
tsum + = temp.data
temp = temp. next
return tsum
if __name__ = = '__main__' :
head = None
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)))
|
C#
using System;
class GFG
{
class Node
{
public int data;
public Node next;
};
static Node push(Node head_ref,
int data)
{
Node ptr1 = new Node();
Node temp = head_ref;
ptr1.data = data;
ptr1.next = head_ref;
if (head_ref != null )
{
while (temp.next != head_ref)
temp = temp.next;
temp.next = ptr1;
}
else
ptr1.next = ptr1;
head_ref = ptr1;
return head_ref;
}
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;
}
public static void Main()
{
Node head = null ;
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));
}
}
|
Javascript
<script>
class Node {
constructor() {
this .data = 0;
this .next = null ;
}
}
function push(head_ref , data) {
var ptr1 = new Node();
var temp = head_ref;
ptr1.data = data;
ptr1.next = head_ref;
if (head_ref != null ) {
while (temp.next != head_ref)
temp = temp.next;
temp.next = ptr1;
} else
ptr1.next = ptr1;
head_ref = ptr1;
return head_ref;
}
function sumOfList(head) {
var temp = head;
var sum = 0;
if (head != null ) {
do {
temp = temp.next;
sum += temp.data;
} while (temp != head);
}
return sum;
}
var head = null ;
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));
</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)
Last Updated :
05 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...