Encuentre el producto de los primeros k Nodes de la lista enlazada dada

Dado un puntero a la cabeza de una lista enlazada individualmente y un entero k . La tarea es encontrar el producto de los primeros k Nodes de la lista enlazada.

Ejemplos: 

Entrada: 10 -> 6 -> 8 -> 4 -> 12, k = 2 
Salida: 60 
10 * 6 = 60

Entrada: 15 -> 7 -> 9 -> 5 -> 16 -> 14, k = 4 
Salida: 4725 
15 * 7 * 9 * 5 = 4725 

Enfoque: establezca prod = 1 (producto requerido) y count = 0 (recuento de Nodes recorridos). Ahora, comience a recorrer los Nodes de la lista vinculada de izquierda a derecha y actualice count = count + 1 y prod = prod * currNode -> data con cada Node recorrido mientras count < k . Imprime el valor de la prod al final.

A continuación se muestra la implementación del enfoque anterior: 

C++

// C++ program to find the product of first
// 'k' nodes of the Linked List
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
 
/* 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 return the product of
// first k nodes of the given linked list
ll product(struct Node* head, int k)
{
    if (k <= 0)
        return 0;
 
    ll prod = 1;
    int i = 0;
    Node* node = head;
 
    // Traverse the list from left to right
    while (i < k) {
 
        // Update product
        prod = prod * node->data;
 
        // Move to the next node
        node = node->next;
        i++;
    }
 
    // Return the required product
    return prod;
}
 
// Driver code
int main()
{
    struct Node* head = NULL;
 
    // Linked list 10 -> 6 -> 8 -> 4 -> 12
    push(&head, 12);
    push(&head, 4);
    push(&head, 8);
    push(&head, 6);
    push(&head, 10);
 
    int k = 2;
    cout << product(head, k);
    return 0;
}

Java

// Java program to find the product of first
// 'k' nodes of the Linked List
class Solution
{
 
/* 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 return the product of
// first k nodes of the given linked list
static long product( Node head, int k)
{
    if (k <= 0)
        return 0;
 
    long prod = 1;
    int i = 0;
    Node node = head;
 
    // Traverse the list from left to right
    while (i < k)
    {
 
        // Update product
        prod = prod * node.data;
 
        // Move to the next node
        node = node.next;
        i++;
    }
 
    // Return the required product
    return prod;
}
 
// Driver code
public static void main(String args[])
{
    Node head = new Node();
 
    // Linked list 10 . 6 . 8 . 4 . 12
    head=push(head, 12);
    head=push(head, 4);
    head=push(head, 8);
    head=push(head, 6);
    head=push(head, 10);
 
    int k = 2;
    System.out.println( product(head, k));
     
}
}
 
// This code is contributed by Arnab Kundu

Python3

     
# Python3 program to find the product
# of first 'k' nodes of the Linked List
import math
 
#define ll long long
 
# 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 return the product of
# first k nodes of the given linked list
def product(head, k):
    if (k <= 0):
        return 0
 
    prod = 1
    i = 0
    node = head
 
    # Traverse the list from left to right
    while (i < k):
 
        # Update product
        prod = prod * node.data
 
        # Move to the next node
        node = node.next
        i = i + 1
     
    # Return the required product
    return prod
 
# Driver code
if __name__=='__main__':
    head = None
 
    # Linked list 10 . 6 . 8 . 4 . 12
    head = push(head, 12);
    head = push(head, 4);
    head = push(head, 8);
    head = push(head, 6);
    head = push(head, 10);
 
    k = 2
    print(product(head, k))
     
# This code is contributed by Srathore

C#

// C# program to find the product of first
// 'k' nodes of the 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 return the product of
// first k nodes of the given linked list
static long product( Node head, int k)
{
    if (k <= 0)
        return 0;
 
    long prod = 1;
    int i = 0;
    Node node = head;
 
    // Traverse the list from left to right
    while (i < k)
    {
 
        // Update product
        prod = prod * node.data;
 
        // Move to the next node
        node = node.next;
        i++;
    }
 
    // Return the required product
    return prod;
}
 
// Driver code
public static void Main()
{
    Node head = new Node();
 
    // Linked list 10 . 6 . 8 . 4 . 12
    head=push(head, 12);
    head=push(head, 4);
    head=push(head, 8);
    head=push(head, 6);
    head=push(head, 10);
 
    int k = 2;
    Console.WriteLine( product(head, k));
}
}
 
// This code is contributed by PrinciRaj1992

Javascript

<script>
// javascript program to find the product of first
// 'k' nodes of the Linked List
 
/* 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 return the product of
    // first k nodes of the given linked list
    function product(head , k) {
        if (k <= 0)
            return 0;
 
        var prod = 1;
        var i = 0;
        var node = head;
 
        // Traverse the list from left to right
        while (i < k) {
 
            // Update product
            prod = prod * node.data;
 
            // Move to the next node
            node = node.next;
            i++;
        }
 
        // Return the required product
        return prod;
    }
 
    // Driver code
     
        var head = new Node();
 
        // Linked list 10 . 6 . 8 . 4 . 12
        head = push(head, 12);
        head = push(head, 4);
        head = push(head, 8);
        head = push(head, 6);
        head = push(head, 10);
 
        var k = 2;
        document.write(product(head, k));
 
 
// This code contributed by Rajput-Ji
</script>
Producción: 

60

 

Publicación traducida automáticamente

Artículo escrito por Akshita207 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 *