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

Given a pointer to the head of a singly linked list and an integer k. The task is to find the product of 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

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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

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 `

Output:

```60
```

My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.