Given a singly linked list. The task is to find the product of all of the nodes of the given linked list.
Input : List = 7->6->8->4->1 Output : Product = 1344 Product of nodes: 7 * 6 * 8 * 4 * 1 = 1344 Input : List = 1->7->3->9->11->5 Output : Product = 10395
- Initialize a pointer ptr with the head of the linked list and a product variable with 1.
- Start traversing the linked list using a loop until all the nodes get traversed.
- Multiply the value of the current node to the product i.e. product *= ptr -> data.
- Increment the pointer to the next node of linked list i.e. ptr = ptr ->next.
- Repeat the above two steps until end of linked list is reached.
- Finally, return the product.
Below is the implementation of above algorithm:
Product = 1344
Time Complexity: O(N), where N is the number of nodes in the linked list.
- Sum and Product of the nodes of a Singly Linked List which are divisible by K
- Sum and Product of all Prime Nodes of a Singly Linked List
- Sum and Product of the nodes of a Circular Singly Linked List which are divisible by K
- Sum of the nodes of a Singly Linked List
- Alternate Odd and Even Nodes in a Singly Linked List
- Count of Prime Nodes of a Singly Linked List
- Reverse alternate K nodes in a Singly Linked List
- Find the common nodes in two singly linked list
- Delete all Prime Nodes from a Singly Linked List
- Delete all Non-Prime Nodes from a Singly Linked List
- Delete all Prime Nodes from a Circular Singly Linked List
- Update adjacent nodes if the current node is zero in a Singly 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
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.
Improved By : andrew1234