Sum and Product of all the nodes which are less than K in the linked list

Given a Linked List and a key K. The task is to calculate the sum and product all the nodes from the list that are lesser than the key K.

Examples:

Input: 12 -> 15 -> 9 -> 11 -> 5 -> 6, K = 9
Output: Sum = 11, Product = 30

Input: 13 -> 4 -> 16 -> 9 -> 22 -> 45 -> 5 -> 16 -> 6, K = 10
Output: Sum = 24, Product = 1080

Approach: Start traversing from the head and check if current node’s value is less than K. If yes, then add that node to the sum and multiply that node for the product and move forward in the list.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to sum and product all the
// nodes from the list that are lesser
// than the specified value K
#include <bits/stdc++.h>
using namespace std;
  
// structure of a node
struct Node {
    int data;
    Node* next;
};
  
// function to get a new node
Node* getNode(int data)
{
    Node* newNode = new Node;
    newNode->data = data;
    newNode->next = NULL;
    return newNode;
}
  
// function to sum all the nodes from the list
// that are lesser than the specified value K
int sumLesserNodes(Node** head_ref, int K)
{
    Node* temp = *head_ref;
  
    int sum = 0;
    while (temp != NULL) {
        if (temp->data < K)
            sum += temp->data;
        temp = temp->next;
    }
  
    return sum;
}
  
// function to product all the nodes from the list
// that are lesser than the specified value K
int productLesserNodes(Node** head_ref, int K)
{
    Node* temp = *head_ref;
  
    int product = 1;
    while (temp != NULL) {
        if (temp->data < K)
            product *= temp->data;
        temp = temp->next;
    }
  
    return product;
}
  
// Driver code
int main()
{
    // Create list: 12->15->9->11->5->6
    Node* head = getNode(12);
    head->next = getNode(15);
    head->next->next = getNode(9);
    head->next->next->next = getNode(11);
    head->next->next->next->next = getNode(5);
    head->next->next->next->next->next = getNode(6);
  
    int K = 9;
    cout << "Sum = " << sumLesserNodes(&head, K) << endl;
    cout << "Product = " << productLesserNodes(&head, K) << endl;
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to sum and product all the
// nodes from the list that are lesser
// than the specified value K
class GFG 
{
  
// structure of a node
static class Node 
{
    int data;
    Node next;
};
  
// function to get a new node
static Node getNode(int data)
{
    Node newNode = new Node();
    newNode.data = data;
    newNode.next = null;
    return newNode;
}
  
// function to sum all the nodes from the list
// that are lesser than the specified value K
static int sumLesserNodes(Node head_ref, int K)
{
    Node temp = head_ref;
  
    int sum = 0;
    while (temp != null
    {
        if (temp.data < K)
            sum += temp.data;
        temp = temp.next;
    }
  
    return sum;
}
  
// function to product all the nodes from the list
// that are lesser than the specified value K
static int productLesserNodes(Node head_ref, int K)
{
    Node temp = head_ref;
  
    int product = 1;
    while (temp != null
    {
        if (temp.data < K)
            product *= temp.data;
        temp = temp.next;
    }
  
    return product;
}
  
// Driver code
public static void main(String[] args) 
{
  
    // Create list: 12->15->9->11->5->6
    Node head = getNode(12);
    head.next = getNode(15);
    head.next.next = getNode(9);
    head.next.next.next = getNode(11);
    head.next.next.next.next = getNode(5);
    head.next.next.next.next.next = getNode(6);
  
    int K = 9;
    System.out.println("Sum = " + sumLesserNodes(head, K));
    System.out.println("Product = " + productLesserNodes(head, K));
}
}
  
// This code has been contributed by 29AjayKumar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to sum and product all the
// nodes from the list that are lesser
// than the specified value K
using System;
      
class GFG 
{
  
// structure of a node
public class Node 
{
    public int data;
    public Node next;
};
  
// function to get a new node
static Node getNode(int data)
{
    Node newNode = new Node();
    newNode.data = data;
    newNode.next = null;
    return newNode;
}
  
// function to sum all the nodes from the list
// that are lesser than the specified value K
static int sumLesserNodes(Node head_ref, int K)
{
    Node temp = head_ref;
  
    int sum = 0;
    while (temp != null
    {
        if (temp.data < K)
            sum += temp.data;
        temp = temp.next;
    }
  
    return sum;
}
  
// function to product all the nodes from the list
// that are lesser than the specified value K
static int productLesserNodes(Node head_ref, int K)
{
    Node temp = head_ref;
  
    int product = 1;
    while (temp != null
    {
        if (temp.data < K)
            product *= temp.data;
        temp = temp.next;
    }
  
    return product;
}
  
// Driver code
public static void Main(String[] args) 
{
  
    // Create list: 12->15->9->11->5->6
    Node head = getNode(12);
    head.next = getNode(15);
    head.next.next = getNode(9);
    head.next.next.next = getNode(11);
    head.next.next.next.next = getNode(5);
    head.next.next.next.next.next = getNode(6);
  
    int K = 9;
    Console.WriteLine("Sum = " + sumLesserNodes(head, K));
    Console.WriteLine("Product = " + productLesserNodes(head, K));
}
}
  
// This code contributed by Rajput-Ji

chevron_right


Output:

Sum = 11
Product = 30

Time Complexity: O(N)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : 29AjayKumar, Rajput-Ji