Skip to content
Related Articles

Related Articles

Improve Article

Find the product of first k nodes of the given Linked List

  • Last Updated : 28 May, 2021

Given a pointer to the head of a singly linked list and an integer k. The task is to find the product of the first k nodes of the linked list.

Examples: 

Input: 10 -> 6 -> 8 -> 4 -> 12, k = 2 
Output: 60 
10 * 6 = 60

Input: 15 -> 7 -> 9 -> 5 -> 16 -> 14, k = 4 
Output: 4725 
15 * 7 * 9 * 5 = 4725 

Approach: Set prod = 1 (required product) and count = 0 (count of nodes traversed). Now, start traversing the nodes of the linked list from left to right and update count = count + 1 and prod = prod * currNode -> data with every traversed node while count < k. Print the value of the prod in the end.

Below is the implementation of the above approach: 

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 povar 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>
Output: 
60

 

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for the language and STL. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.



My Personal Notes arrow_drop_up
Recommended Articles
Page :