Sum of all subset sums of a linked list
Last Updated :
01 Feb, 2023
Given a linked list, the task is to find the sum of all subsets of a linked list.
Examples:
Input: 2 -> 3 -> NULL
Output: 10
Explanation:
All non-empty subsets are {2}, {3} and {2, 3}
Total sum = 2 + 3 + (2 + 3) = 10
Input: 2 -> 1 -> 5 -> 6 -> NULL
Output: 112
Approach: Considering all the possible subsets, we can observe that each node appears 2(N – 1) times. Thus, the product of sum of all nodes and 2(N – 1) gives us the final answer.
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 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;
int n = 0;
while (ptr != NULL) {
sum += ptr->data;
ptr = ptr->next;
n++;
}
sum = sum * pow (2, n - 1);
return sum;
}
int main()
{
struct Node* head = NULL;
push(&head, 2);
push(&head, 1);
push(&head, 5);
push(&head, 6);
cout << sumOfNodes(head);
return 0;
}
|
Java
import java.util.*;
class GFG{
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 int sumOfNodes(Node head)
{
Node ptr = head;
int sum = 0 ;
int n = 0 ;
while (ptr != null ) {
sum += ptr.data;
ptr = ptr.next;
n++;
}
sum = ( int ) (sum * Math.pow( 2 , n - 1 ));
return sum;
}
public static void main(String[] args)
{
Node head = null ;
head = push(head, 2 );
head = push(head, 1 );
head = push(head, 5 );
head = push(head, 6 );
System.out.print(sumOfNodes(head));
}
}
|
Python3
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. next = head_ref
head_ref = new_node
return head_ref
def sumOfNodes(head):
ptr = head
sum = 0
n = 0
while (ptr ! = None ) :
sum + = ptr.data
ptr = ptr. next
n + = 1
sum = sum * pow ( 2 , n - 1 )
return sum
if __name__ = = '__main__' :
head = None
head = push(head, 2 )
head = push(head, 1 )
head = push(head, 5 )
head = push(head, 6 )
print (sumOfNodes(head))
|
C#
using System;
class GFG{
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 int sumOfNodes(Node head)
{
Node ptr = head;
int sum = 0;
int n = 0;
while (ptr != null ) {
sum += ptr.data;
ptr = ptr.next;
n++;
}
sum = ( int ) (sum * Math.Pow(2, n - 1));
return sum;
}
public static void Main(String[] args)
{
Node head = null ;
head = push(head, 2);
head = push(head, 1);
head = push(head, 5);
head = push(head, 6);
Console.Write(sumOfNodes(head));
}
}
|
Javascript
<script>
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_ref;
}
function sumOfNodes(head)
{
var ptr = head;
var sum = 0;
var n = 0;
while (ptr != null ) {
sum += ptr.data;
ptr = ptr.next;
n++;
}
sum = sum * Math.pow(2, n - 1);
return sum;
}
var head = null ;
head = push(head, 2);
head = push(head, 1);
head = push(head, 5);
head = push(head, 6);
document.write( sumOfNodes(head));
</script>
|
Time Complexity: O(N)
The time complexity of this algorithm is O(N) as we need to traverse the linked list with n nodes to calculate the sum.
Space Complexity: O(1).
No extra space is required to store any values as all values are calculated on the go.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...