Find the product of first k nodes of the given Linked List
Last Updated :
15 Sep, 2022
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++
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
struct Node {
int data;
struct Node* next;
};
void push( struct Node** head_ref, int new_data)
{
struct Node* new_node = new Node;
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
ll product( struct Node* head, int k)
{
if (k <= 0)
return 0;
ll prod = 1;
int i = 0;
Node* node = head;
while (i < k) {
prod = prod * node->data;
node = node->next;
i++;
}
return prod;
}
int main()
{
struct Node* head = NULL;
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
class Solution
{
static class Node
{
int data;
Node next;
}
static Node push( Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = (head_ref);
(head_ref) = new_node;
return head_ref;
}
static long product( Node head, int k)
{
if (k <= 0 )
return 0 ;
long prod = 1 ;
int i = 0 ;
Node node = head;
while (i < k)
{
prod = prod * node.data;
node = node.next;
i++;
}
return prod;
}
public static void main(String args[])
{
Node head = new Node();
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));
}
}
|
Python3
import math
class Node:
def __init__( self , data):
self .data = data
self . next = None
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
def product(head, k):
if (k < = 0 ):
return 0
prod = 1
i = 0
node = head
while (i < k):
prod = prod * node.data
node = node. next
i = i + 1
return prod
if __name__ = = '__main__' :
head = None
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))
|
C#
using System;
class GFG
{
class Node
{
public int data;
public Node next;
}
static Node push( Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = (head_ref);
(head_ref) = new_node;
return head_ref;
}
static long product( Node head, int k)
{
if (k <= 0)
return 0;
long prod = 1;
int i = 0;
Node node = head;
while (i < k)
{
prod = prod * node.data;
node = node.next;
i++;
}
return prod;
}
public static void Main()
{
Node head = new Node();
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));
}
}
|
Javascript
<script>
class Node {
constructor(val) {
this .data = val;
this .next = null ;
}
}
function push(head_ref , new_data) {
var new_node = new Node();
new_node.data = new_data;
new_node.next = (head_ref);
(head_ref) = new_node;
return head_ref;
}
function product(head , k) {
if (k <= 0)
return 0;
var prod = 1;
var i = 0;
var node = head;
while (i < k) {
prod = prod * node.data;
node = node.next;
i++;
}
return prod;
}
var head = new Node();
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));
</script>
|
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...