Given a singly linked list. The task is to find the sum of nodes of the given linked list.

Task is to do A + B + C + D.
Examples:
Input: 7->6->8->4->1
Output: 26
Sum of nodes:
7 + 6 + 8 + 4 + 1 = 26
Input: 1->7->3->9->11->5
Output: 36
Recursive Solution:
- Call a function by passing the head and variable to store the sum.
- Then recursively call the function by passing the next of current node and sum variable.
- Add the value of the current node to the sum.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};
void push( struct Node** head_ref, int new_data)
{
struct Node* new_node = new Node;
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void sumOfNodes( struct Node* head, int * sum)
{
if (!head)
return ;
sumOfNodes(head->next, sum);
*sum = *sum + head->data;
}
int sumOfNodesUtil( struct Node* head)
{
int sum = 0;
sumOfNodes(head, &sum);
return sum;
}
int main()
{
struct Node* head = NULL;
push(&head, 7);
push(&head, 6);
push(&head, 8);
push(&head, 4);
push(&head, 1);
cout << "Sum of nodes = "
<< sumOfNodesUtil(head);
return 0;
}
|
Java
class GFG
{
static int sum= 0 ;
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 void sumOfNodes( Node head)
{
if (head == null )
return ;
sumOfNodes(head.next);
sum = sum + head.data;
}
static int sumOfNodesUtil( Node head)
{
sum = 0 ;
sumOfNodes(head);
return sum;
}
public static void main(String args[])
{
Node head = null ;
head = push(head, 7 );
head = push(head, 6 );
head = push(head, 8 );
head = push(head, 4 );
head = push(head, 1 );
System.out.println( "Sum of nodes = "
+ sumOfNodesUtil(head));
}
}
|
Python3
import math
class Sum :
tsum = None
class Node:
def __init__( self ,data):
self .data = data
self . next = None
def push(head, data):
if not head:
return Node(data)
new_node = Node(data)
new_node. next = head
head = new_node
return head
def sumOfNode(head, S):
if not head:
return
sumOfNode(head. next , S)
S.tsum + = head.data
def sumOfNodesUtil(head):
S = Sum ()
S.tsum = 0
sumOfNode(head, S)
return S.tsum
if __name__ = = '__main__' :
head = None
head = push(head, 7 )
head = push(head, 6 )
head = push(head, 8 )
head = push(head, 4 )
head = push(head, 1 )
print ( "Sum of Nodes = {}" .
format (sumOfNodesUtil(head)))
|
C#
using System;
class GFG
{
public static int sum = 0;
public 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 void sumOfNodes(Node head)
{
if (head == null )
return ;
sumOfNodes(head.next);
sum = sum + head.data;
}
static int sumOfNodesUtil(Node head)
{
sum = 0;
sumOfNodes(head);
return sum;
}
public static void Main(String[] args)
{
Node head = null ;
head = push(head, 7);
head = push(head, 6);
head = push(head, 8);
head = push(head, 4);
head = push(head, 1);
Console.WriteLine( "Sum of nodes = " +
sumOfNodesUtil(head));
}
}
|
Javascript
<script>
var sum = 0;
class Node {
constructor(val) {
this .data = val;
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 sumOfNodes(head) {
if (head == null )
return ;
sumOfNodes(head.next);
sum = sum + head.data;
}
function sumOfNodesUtil(head) {
sum = 0;
sumOfNodes(head);
return sum;
}
var head = null ;
head = push(head, 7);
head = push(head, 6);
head = push(head, 8);
head = push(head, 4);
head = push(head, 1);
document.write( "Sum of nodes = " +
sumOfNodesUtil(head));
</script>
|
Output: Sum of nodes = 26
Time Complexity: O(N) , N is the number of nodes in a linked list.
Auxiliary Space: O(N), only if the stack size is considered during recursive calls.
Iterative Solution:
- Initialize a pointer ptr 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 += ptr -> data .
- Increment the pointer to the next node of linked list i.e. ptr = ptr ->next .
- Return the sum.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};
void push( struct Node** head_ref, int new_data)
{
struct Node* new_node = new Node;
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int sumOfNodes( struct Node* head)
{
struct Node* ptr = head;
int sum = 0;
while (ptr != NULL) {
sum += ptr->data;
ptr = ptr->next;
}
return sum;
}
int main()
{
struct Node* head = NULL;
push(&head, 7);
push(&head, 6);
push(&head, 8);
push(&head, 4);
push(&head, 1);
cout << "Sum of nodes = "
<< sumOfNodes(head);
return 0;
}
|
Java
class GFG
{
static Node head;
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=head_ref;
}
static int sumOfNodes( Node head)
{
Node ptr = head;
int sum = 0 ;
while (ptr != null )
{
sum += ptr.data;
ptr = ptr.next;
}
return sum;
}
public static void main(String[] args)
{
push(head, 7 );
push(head, 6 );
push(head, 8 );
push(head, 4 );
push(head, 1 );
System.out.println( "Sum of nodes = "
+ sumOfNodes(head));
}
}
|
Python3
import math
class Node:
def __init__( self , data):
self .data = data
self . next = None
def push(head_ref, new_data):
new_node = Node(new_data)
new_node.data = new_data
new_node. next = head_ref
head_ref = new_node
return head_ref
def sumOfNodes(head):
ptr = head
sum = 0
while (ptr ! = None ):
sum = sum + ptr.data
ptr = ptr. next
return sum
if __name__ = = '__main__' :
head = None
head = push(head, 7 )
head = push(head, 6 )
head = push(head, 8 )
head = push(head, 4 )
head = push(head, 1 )
print ( "Sum of nodes =" ,
sumOfNodes(head))
|
C#
using System;
class GFG
{
static Node head;
public 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=head_ref;
}
static int sumOfNodes( Node head)
{
Node ptr = head;
int sum = 0;
while (ptr != null )
{
sum += ptr.data;
ptr = ptr.next;
}
return sum;
}
public static void Main(String[] args)
{
push(head, 7);
push(head, 6);
push(head, 8);
push(head, 4);
push(head, 1);
Console.WriteLine( "Sum of nodes = "
+ sumOfNodes(head));
}
}
|
Javascript
<script>
var head;
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 = head_ref;
}
function sumOfNodes(head) {
var ptr = head;
var sum = 0;
while (ptr != null ) {
sum += ptr.data;
ptr = ptr.next;
}
return sum;
}
push(head, 7);
push(head, 6);
push(head, 8);
push(head, 4);
push(head, 1);
document.write( "Sum of nodes = " + sumOfNodes(head));
</script>
|
Output: Sum of nodes = 26
Time Complexity: O(N), N is the number of nodes in a linked list.
Auxiliary Space: O(1)