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.
Input: 10 -> 6 -> 8 -> 4 -> 12, k = 2
10 * 6 = 60
Input: 15 -> 7 -> 9 -> 5 -> 16 -> 14, k = 4
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 prod in the end.
Below is the implementation of the above approach:
- Find the product of last N nodes of the given Linked List
- Sum and Product of all the nodes which are less than K in the linked list
- Linked List Product of Nodes Between 0s
- Product of the alternate nodes of linked list
- Product of the nodes of a Singly Linked List
- Sum and Product of all Prime Nodes of a Singly Linked List
- Sum and Product of the nodes of a Singly Linked List which are divisible by K
- Product of all prime nodes in a Doubly Linked List
- Sum and Product of the nodes of a Circular Singly Linked List which are divisible by K
- Product of all nodes in a doubly linked list divisible by a given number K
- Find sum of even and odd nodes in a linked list
- Find the sum of last n nodes of the given Linked List
- Program to find average of all nodes in a Linked List
- Find the common nodes in two singly linked list
- Find pairs with given product in a sorted Doubly Linked List
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.