Sum of the nodes of a Singly Linked List
Last Updated :
11 Jan, 2023
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++
// C++ implementation to find the sum of
// nodes of the Linked List
#include <bits/stdc++.h>
using namespace std;
/* A Linked list node */
struct Node {
int data;
struct Node* next;
};
// function to insert a node at the
// beginning of the linked list
void push(struct Node** head_ref, int new_data)
{
/* allocate node */
struct Node* new_node = new Node;
/* put in the data */
new_node->data = new_data;
/* link the old list to the new node */
new_node->next = (*head_ref);
/* move the head to point to the new node */
(*head_ref) = new_node;
}
// function to recursively find the sum of
// nodes of the given linked list
void sumOfNodes(struct Node* head, int* sum)
{
// if head = NULL
if (!head)
return;
// recursively traverse the remaining nodes
sumOfNodes(head->next, sum);
// accumulate sum
*sum = *sum + head->data;
}
// utility function to find the sum of nodes
int sumOfNodesUtil(struct Node* head)
{
int sum = 0;
// find the sum of nodes
sumOfNodes(head, &sum);
// required sum
return sum;
}
// Driver program to test above
int main()
{
struct Node* head = NULL;
// create linked list 7->6->8->4->1
push(&head, 7);
push(&head, 6);
push(&head, 8);
push(&head, 4);
push(&head, 1);
cout << "Sum of nodes = "
<< sumOfNodesUtil(head);
return 0;
}
Java
// Java implementation to find the sum of
// nodes of the Linked List
class GFG
{
static int sum=0;
// A Linked list node /
static class Node
{
int data;
Node next;
}
// function to insert a node at the
// beginning of the linked list
static Node push( Node head_ref, int new_data)
{
// allocate node /
Node new_node = new Node();
// put in the data /
new_node.data = new_data;
// link the old list to the new node /
new_node.next = (head_ref);
// move the head to point to the new node /
(head_ref) = new_node;
return head_ref;
}
// function to recursively find the sum of
// nodes of the given linked list
static void sumOfNodes( Node head)
{
// if head = null
if (head == null)
return;
// recursively traverse the remaining nodes
sumOfNodes(head.next);
// accumulate sum
sum = sum + head.data;
}
// utility function to find the sum of nodes
static int sumOfNodesUtil( Node head)
{
sum = 0;
// find the sum of nodes
sumOfNodes(head);
// required sum
return sum;
}
// Driver program to test above
public static void main(String args[])
{
Node head = null;
// create linked list 7.6.8.4.1
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));
}
}
// This code is contributed by Arnab Kundu
Python3
# Python3 implementation to find the sum of
# nodes of the Linked List
import math
# class for a Sum
class Sum:
tsum = None
# A Linked list node
class Node:
def __init__(self,data):
self.data = data
self.next = None
# function to insert a node at the
# beginning of the linked list
def push(head, data):
if not head:
return Node(data)
# put in the data
# and allocate node
new_node = Node(data)
# link the old list to the new node
new_node.next = head
# move the head to point
# to the new node
head = new_node
return head
# function to recursively find
# the sum of nodes of the given
# linked list
def sumOfNode(head, S):
# if head = NULL
if not head:
return
# recursively traverse the
# remaining nodes
sumOfNode(head.next, S)
# accumulate sum
S.tsum += head.data
# utility function to find
# the sum of nodes
def sumOfNodesUtil(head):
S = Sum()
S.tsum = 0
# find the sum of nodes
sumOfNode(head, S)
# required sum
return S.tsum
# Driver Code
if __name__=='__main__':
head = None
# create linked list 7->6->8->4->1
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)))
# This code is contributed
# by Vikash Kumar 37
C#
// C# implementation to find the sum of
// nodes of the Linked List
using System;
class GFG
{
public static int sum = 0;
// A Linked list node /
public class Node
{
public int data;
public Node next;
}
// function to insert a node at the
// beginning of the linked list
static Node push(Node head_ref, int new_data)
{
// allocate node /
Node new_node = new Node();
// put in the data /
new_node.data = new_data;
// link the old list to the new node /
new_node.next = (head_ref);
// move the head to point to the new node /
(head_ref) = new_node;
return head_ref;
}
// function to recursively find the sum of
// nodes of the given linked list
static void sumOfNodes(Node head)
{
// if head = null
if (head == null)
return;
// recursively traverse the remaining nodes
sumOfNodes(head.next);
// accumulate sum
sum = sum + head.data;
}
// utility function to find the sum of nodes
static int sumOfNodesUtil(Node head)
{
sum = 0;
// find the sum of nodes
sumOfNodes(head);
// required sum
return sum;
}
// Driver program to test above
public static void Main(String[] args)
{
Node head = null;
// create linked list 7.6.8.4.1
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));
}
}
// This code is contributed by Rajput-Ji
JavaScript
<script>
// JavaScript implementation to find the sum of
// nodes of the Linked List
var sum = 0;
// A Linked list node /
class Node {
constructor(val) {
this.data = val;
this.next = null;
}
}
// function to insert a node at the
// beginning of the linked list
function push(head_ref , new_data) {
// allocate node /
var new_node = new Node();
// put in the data /
new_node.data = new_data;
// link the old list to the new node /
new_node.next = (head_ref);
// move the head to point to the new node /
(head_ref) = new_node;
return head_ref;
}
// function to recursively find the sum of
// nodes of the given linked list
function sumOfNodes(head) {
// if head = null
if (head == null)
return;
// recursively traverse the remaining nodes
sumOfNodes(head.next);
// accumulate sum
sum = sum + head.data;
}
// utility function to find the sum of nodes
function sumOfNodesUtil(head) {
sum = 0;
// find the sum of nodes
sumOfNodes(head);
// required sum
return sum;
}
// Driver program to test above
var head = null;
// create linked list 7.6.8.4.1
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));
// This code contributed by umadevi9616
</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++
// C++ implementation to find the sum of
// nodes of the Linked List
#include <bits/stdc++.h>
using namespace std;
/* A Linked list node */
struct Node {
int data;
struct Node* next;
};
// function to insert a node at the
// beginning of the linked list
void push(struct Node** head_ref, int new_data)
{
/* allocate node */
struct Node* new_node = new Node;
/* put in the data */
new_node->data = new_data;
/* link the old list to the new node */
new_node->next = (*head_ref);
/* move the head to point to the new node */
(*head_ref) = new_node;
}
// function to find the sum of
// nodes of the given linked list
int sumOfNodes(struct Node* head)
{
struct Node* ptr = head;
int sum = 0;
while (ptr != NULL) {
sum += ptr->data;
ptr = ptr->next;
}
return sum;
}
// Driver program to test above
int main()
{
struct Node* head = NULL;
// create linked list 7->6->8->4->1
push(&head, 7);
push(&head, 6);
push(&head, 8);
push(&head, 4);
push(&head, 1);
cout << "Sum of nodes = "
<< sumOfNodes(head);
return 0;
}
Java
// Java implementation to find the sum of
// nodes of the Linked List
class GFG
{
static Node head;
/* A Linked list node */
static class Node
{
int data;
Node next;
};
// function to insert a node at the
// beginning of the linked list
// Inserting node at the beginning
static Node push(Node head_ref, int new_data)
{
/* allocate node */
Node new_node = new Node();
/* put in the data */
new_node.data = new_data;
/* link the old list to the new node */
new_node.next = head_ref;
/* move the head to point to the new node */
head_ref = new_node;
return head=head_ref;
}
// function to find the sum of
// nodes of the given linked list
static int sumOfNodes( Node head)
{
Node ptr = head;
int sum = 0;
while (ptr != null)
{
sum += ptr.data;
ptr = ptr.next;
}
return sum;
}
// Driver code
public static void main(String[] args)
{
// create linked list 7.6.8.4.1
push(head, 7);
push(head, 6);
push(head, 8);
push(head, 4);
push(head, 1);
System.out.println("Sum of nodes = "
+ sumOfNodes(head));
}
}
/* This code is contributed by PrinciRaj1992 */
Python3
# Python3 implementation to find the
# sum of nodes of the Linked List
import math
# A Linked list node
class Node:
def __init__(self, data):
self.data = data
self.next = None
# function to insert a node at the
# beginning of the linked list
def push(head_ref, new_data):
# allocate node
new_node = Node(new_data)
# put in the data
new_node.data = new_data
# link the old list to the new node
new_node.next = head_ref
# move the head to po to the new node
head_ref = new_node
return head_ref
# function to find the sum of
# nodes of the given linked list
def sumOfNodes(head):
ptr = head
sum = 0
while (ptr != None):
sum = sum + ptr.data
ptr = ptr.next
return sum
# Driver Code
if __name__=='__main__':
head = None
# create linked list 7.6.8.4.1
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))
# This code is contributed by Srathore
C#
// C# implementation to find the sum of
// nodes of the Linked List
using System;
class GFG
{
static Node head;
/* A Linked list node */
public class Node
{
public int data;
public Node next;
};
// function to insert a node at the
// beginning of the linked list
// Inserting node at the beginning
static Node push(Node head_ref, int new_data)
{
/* allocate node */
Node new_node = new Node();
/* put in the data */
new_node.data = new_data;
/* link the old list to the new node */
new_node.next = head_ref;
/* move the head to point to the new node */
head_ref = new_node;
return head=head_ref;
}
// function to find the sum of
// nodes of the given linked list
static int sumOfNodes( Node head)
{
Node ptr = head;
int sum = 0;
while (ptr != null)
{
sum += ptr.data;
ptr = ptr.next;
}
return sum;
}
// Driver code
public static void Main(String[] args)
{
// create linked list 7.6.8.4.1
push(head, 7);
push(head, 6);
push(head, 8);
push(head, 4);
push(head, 1);
Console.WriteLine("Sum of nodes = "
+ sumOfNodes(head));
}
}
// This code is contributed by 29AjayKumar
JavaScript
<script>
// javascript implementation to find the sum of
// nodes of the Linked List
var head;
/* A Linked list node */
class Node {
constructor() {
this.data = 0;
this.next = null;
}
}
// function to insert a node at the
// beginning of the linked list
// Inserting node at the beginning
function push(head_ref , new_data) {
/* allocate node */
var new_node = new Node();
/* put in the data */
new_node.data = new_data;
/* link the old list to the new node */
new_node.next = head_ref;
/* move the head to point to the new node */
head_ref = new_node;
return head = head_ref;
}
// function to find the sum of
// nodes of the given linked list
function sumOfNodes(head) {
var ptr = head;
var sum = 0;
while (ptr != null) {
sum += ptr.data;
ptr = ptr.next;
}
return sum;
}
// Driver code
// create linked list 7.6.8.4.1
push(head, 7);
push(head, 6);
push(head, 8);
push(head, 4);
push(head, 1);
document.write("Sum of nodes = " + sumOfNodes(head));
// This code contributed by aashish1995
</script>
Output: Sum of nodes = 26
Time Complexity: O(N), N is the number of nodes in a linked list.
Auxiliary Space: O(1)
Similar Reads
Sum of the alternate nodes of linked list Given a linked list, the task is to print the sum of the alternate nodes of the linked list. Examples: Input : 1 -> 8 -> 3 -> 10 -> 17 -> 22 -> 29 -> 42 Output : 50 Alternate nodes : 1 -> 3 -> 17 -> 29 Input : 10 -> 17 -> 33 -> 38 -> 73 Output : 116 Alternat
12 min read
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 C
7 min read
Sum of all odd frequency nodes of the Linked List Given a linked list, the task is to find the sum of all the odd frequency nodes from the given linked list.Examples: Input: 8 -> 8 -> 1 -> 4 -> 1 -> 2 -> 8 -> NULL Output: 30 freq(8) = 3 freq(1) = 2 freq(4) = 1 freq(2) = 1 8, 4 and 2 appear odd number of times and (8 * 3) + (4 *
7 min read
Subtraction of the alternate nodes of Linked List Given a linked list. The task is to print the difference between the first odd-positioned node with the sum of all other odd-positioned nodes. Examples: Input : 1 -> 8 -> 3 -> 10 -> 17 -> 22 -> 29 -> 42 Output : -48 Alternate nodes : 1 -> 3 -> 17 -> 29 1 - (3 + 17 + 29)
12 min read
Sum and Product of all Prime Nodes of a Singly Linked List Given a singly linked list containing N nodes, the task is to find the sum and product of all nodes from the list which are prime. Examples: Input : List = 15 -> 16 -> 6 -> 7 -> 17 Output : Product = 119, Sum = 24 Prime nodes are 7, 17. Input : List = 15 -> 3 -> 4 -> 2 -> 9 O
15+ min read
Sum of smaller elements of nodes in a linked list Given a linked list, every node of which consists of a pair of integer variables first and second to hold the data, and a pointer pointing to the next node in the list. The task is to find the sum of min(first, second) for every node.Examples: Input: (2, 3) -> (3, 4) - > (1, 10) -> (20, 15)
5 min read
Find the sum of last n nodes of the given Linked List Given a linked list and a number n. Find the sum of the last n nodes of the linked list.Constraints: 0 <= n <= number of nodes in the linked list. Examples: Input : 10->6->8->4->12, n = 2 Output : 16 Sum of last two nodes: 12 + 4 = 16 Input : 15->7->9->5->16->14, n =
15+ min read
Sum and Product of all Fibonacci Nodes of a Singly Linked List Given a singly linked list containing N nodes, the task is to find the sum and product of all the nodes from the list whose data value is a Fibonacci number. Examples: Input: LL = 15 -> 16 -> 8 -> 6 -> 13 Output: Sum = 21, Product = 104 Explanation: The list contains 2 fibonacci data val
13 min read
Sum and Product of all even digit sum Nodes of a Singly Linked List Given a singly linked list containing N nodes, the task is to find the sum and product of all the nodes from the list whose data value has an even digit sum. Examples: Input: 15 -> 16 -> 8 -> 6 -> 13 Output: Sum = 42 Product = 9360 Explanation: The sum of all digit of number in linked li
15+ min read
Sum and Product of the nodes of a Singly Linked List which are divisible by K Given a singly linked list. The task is to find the sum and product of all of the nodes of the given linked list which are divisible by a given number k. Examples: Input : List = 7->60->8->40->1 k = 10 Output : Product = 2400, Sum = 100Product of nodes: 60 * 40 = 2400Input : List = 15-
15 min read