Given a Linked List and a key K. The task is to calculate the sum and product all the nodes from the list that are lesser than the key K.
Input: 12 -> 15 -> 9 -> 11 -> 5 -> 6, K = 9
Output: Sum = 11, Product = 30
Input: 13 -> 4 -> 16 -> 9 -> 22 -> 45 -> 5 -> 16 -> 6, K = 10
Output: Sum = 24, Product = 1080
Approach: Start traversing from the head and check if current node’s value is less than K. If yes, then add that node to the sum and multiply that node for the product and move forward in the list.
Below is the implementation of the above approach:
Sum = 11 Product = 30
Time Complexity: O(N)
- Linked List Product of Nodes Between 0s
- Find the product of last N nodes of the given Linked List
- Product of the nodes of a Singly Linked List
- Product of the alternate nodes of linked list
- Find the product of first k nodes of the given Linked List
- Sum and Product of all Prime Nodes of a Singly Linked List
- Product of all prime nodes in a Doubly Linked List
- Sum and Product of the nodes of a Singly Linked List which are divisible by K
- 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
- Append odd position nodes in reverse at the end of even positioned nodes in a Linked List
- Splitting starting N nodes into new Circular Linked List while preserving the old nodes
- Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes
- Delete N nodes after M nodes of a linked list
- Linked List Sum of Nodes Between 0s
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.