📜  链表的所有子集和的总和

📅  最后修改于: 2021-09-07 05:28:07             🧑  作者: Mango

给定一个链表,任务是找到一个链表的所有子集的总和。

例子:

方法:考虑所有可能的子集,我们可以观察到每个节点出现2 (N – 1)次。因此,所有节点的总和与 2 (N – 1)的乘积给出了最终答案。

下面是上述方法的实现:

C++
// C++ implementation to find the
// sum of values of all subsets of linked list.
#include 
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 values of all subsets of linked list.
int sumOfNodes(struct Node* head)
{
    struct Node* ptr = head;
    int sum = 0;
    int n = 0; // size of linked list
    while (ptr != NULL) {
  
        sum += ptr->data;
        ptr = ptr->next;
        n++;
    }
  
    // Every element appears 2^(n-1) times
    sum = sum * pow(2, n - 1);
    return sum;
}
  
// Driver program to test above
int main()
{
    struct Node* head = NULL;
  
    // create linked list 2->1->5->6
    push(&head, 2);
    push(&head, 1);
    push(&head, 5);
    push(&head, 6);
  
    cout << sumOfNodes(head);
    return 0;
}


Java
// Java implementation to find the
// sum of values of all subsets of linked list.
  
import java.util.*;
  
class GFG{
   
/* 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 find the
// sum of values of all subsets of linked list.
static int sumOfNodes(Node head)
{
    Node ptr = head;
    int sum = 0;
    int n = 0; // size of linked list
    while (ptr != null) {
   
        sum += ptr.data;
        ptr = ptr.next;
        n++;
    }
   
    // Every element appears 2^(n-1) times
    sum = (int) (sum * Math.pow(2, n - 1));
    return sum;
}
   
// Driver program to test above
public static void main(String[] args)
{
    Node head = null;
   
    // create linked list 2.1.5.6
    head = push(head, 2);
    head = push(head, 1);
    head = push(head, 5);
    head = push(head, 6);
   
    System.out.print(sumOfNodes(head));
}
}
  
// This code is contributed by 29AjayKumar


Python3
# Python3 implementation to find the
# sum of values of all subsets of linked list.
  
# 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):
    new_node=Node(new_data)
    #new_node.data = new_data
    new_node.next = head_ref
    head_ref = new_node
    return head_ref
  
# function to find the
# sum of values of all subsets of linked list.
def sumOfNodes(head):
    ptr = head
    sum = 0
    n = 0 # size of linked list
    while (ptr != None) :
  
        sum += ptr.data
        ptr = ptr.next
        n += 1
      
    # Every element appears 2^(n-1) times
    sum = sum * pow(2, n - 1)
    return sum
  
# Driver program to test above
if __name__=='__main__': 
  
    head = None
  
    # create linked list 2.1.5.6
    head = push(head, 2)
    head = push(head, 1)
    head = push(head, 5)
    head = push(head, 6)
  
    print(sumOfNodes(head))
      
# This code is contributed by AbhiThakur


C#
// C# implementation to find the
// sum of values of all subsets of linked list.
using System;
  
class GFG{
    
/* A Linked list node */
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 find the
// sum of values of all subsets of linked list.
static int sumOfNodes(Node head)
{
    Node ptr = head;
    int sum = 0;
    int n = 0; // size of linked list
    while (ptr != null) {
    
        sum += ptr.data;
        ptr = ptr.next;
        n++;
    }
    
    // Every element appears 2^(n-1) times
    sum = (int) (sum * Math.Pow(2, n - 1));
    return sum;
}
    
// Driver program to test above
public static void Main(String[] args)
{
    Node head = null;
    
    // create linked list 2.1.5.6
    head = push(head, 2);
    head = push(head, 1);
    head = push(head, 5);
    head = push(head, 6);
    
    Console.Write(sumOfNodes(head));
}
}
  
// This code is contributed by Rajput-Ji


输出:
112

时间复杂度: O(N)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live