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)
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Linked List Product of Nodes Between 0s
- Product of the nodes of a Singly Linked List
- Find the product of first k nodes of the given Linked List
- Find the product of last N nodes of the given Linked List
- Product of the alternate nodes of linked list
- Sum and Product of nodes with value as even digit sum in Circular Linked List
- Sum and Product of all even digit sum Nodes of a Singly 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
- Product of all prime nodes in a Doubly Linked List
- Sum and Product of all Fibonacci Nodes of a Singly Linked List
- Product of all nodes in a doubly linked list divisible by a given number K
- Sum and Product of the nodes of a Circular Singly Linked List which are divisible by K
- Splitting starting N nodes into new Circular Linked List while preserving the old nodes
- Append odd position nodes in reverse at the end of even positioned nodes in a Linked List
- 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
- Sum of all distinct nodes in a linked list
- Sum 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.