Suma y Producto de todos los Nodes que son menores que K en la lista enlazada

Dada una Lista Enlazada y una clave K. La tarea es calcular la suma y el producto de todos los Nodes de la lista que son menores que la clave K.
Ejemplos: 
 

Entrada: 12 -> 15 -> 9 -> 11 -> 5 -> 6, K = 9 
Salida: Suma = 11, Producto = 30
Entrada: 13 -> 4 -> 16 -> 9 -> 22 -> 45 – > 5 -> 16 -> 6, K = 10 
Salida: Suma = 24, Producto = 1080 
 

Enfoque: comience a atravesar desde la cabeza y verifique si el valor del Node actual es menor que K. En caso afirmativo, agregue ese Node a la suma y multiplique ese Node por el producto y avance en la lista.
A continuación se muestra la implementación del enfoque anterior: 
 

C++

// 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;
}

Java

// 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

Python3

# Python3 program to sum and product all the
# nodes from the list that are lesser
# than the specified value K
 
# Node of the single linked list
class Node:
     
    def __init__(self, data):
        self.data = data
        self.next = None
 
# function to get a new node
def getNode(data):
 
    newNode = Node(0)
    newNode.data = data
    newNode.next = None
    return newNode
 
# function to sum all the nodes from the list
# that are lesser than the specified value K
def sumLesserNodes(head_ref, K):
 
    temp = head_ref
 
    sum = 0
    while (temp != None) :
        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
def productLesserNodes(head_ref,K):
 
    temp = head_ref
 
    product = 1
    while (temp != None) :
        if (temp.data < K):
            product *= temp.data
        temp = temp.next
     
    return product
 
# Driver Code
if __name__ == "__main__":
 
    # Create list: 12.15.9.11.5.6
    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)
 
    K = 9
    print("Sum =", sumLesserNodes(head, K))
    print("Product =", productLesserNodes(head, K))
 
# This code is contributed by Arnab Kundu

C#

// 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

Javascript

<script>
 
// JavaScript program to sum and product all the
// nodes from the list that are lesser
// than the specified value K
 
class Node
{
    constructor()
    {
        this.data=0;
        this.next=null;
    }
}
 
// function to get a new node
function getNode(data)
{   
    let 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
function sumLesserNodes(head_ref,K)
{
    let temp = head_ref;
   
    let 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
function productLesserNodes(head_ref,K)
{
    let temp = head_ref;
   
    let product = 1;
    while (temp != null)
    {
        if (temp.data < K)
            product *= temp.data;
        temp = temp.next;
    }
   
    return product;
}
 
// Driver code
// Create list: 12->15->9->11->5->6
let 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);
 
let K = 9;
document.write("Sum = " + sumLesserNodes(head, K)+"<br>");
document.write("Product = " + productLesserNodes(head, K)+"<br>");
 
 
// This code is contributed by avanitrachhadiya2155
 
</script>
Producción: 

Sum = 11
Product = 30

 

Complejidad de tiempo: O(N)
 

Publicación traducida automáticamente

Artículo escrito por VishalBachchas y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *