Given a circular singly linked list containing N nodes, The task is to find the sum and product of all the nodes from the list whose data value has an even digit sum.
Input: List = 15 -> 16 -> 8 -> 6 -> 13
Output: Sum = 42, Product = 9360
The circular linked list contains:
15 -> 1 + 5 = 6
16 -> 1 + 6 = 7
8 -> 8
6 -> 6
13 -> 1 + 3 = 4
The list contains 4 Even Digit Sum data values 15, 8, 6 and 13.
Sum = 15 + 8 + 6 + 13 = 42
Product = 15 * 8 * 6 * 13 = 9360
Input: List = 5 -> 3 -> 4 -> 2 -> 9
Output: Sum = 6, Product = 8
The list contains 2 Even Digit Sum data values 4 and 2.
Sum = 4 + 2 = 6
Product = 4 * 2 = 8
- Initialize a pointer current with the head of the circular linked list and a sum variable sum with 0 and a product variable product with 1.
- Start traversing the linked list using a do-while loop until all the nodes get traversed.
- If current node data value has an even digit sum.
- Add the value of current node to the sum i.e. sum = sum + current -> data.
- Multiply the value of current node to the product i.e. product = product * current -> data.
- Increment the pointer to the next node of linked list i.e. temp = temp -> next.
- Print the sum and product.
Below is the implementation of the above approach:
Initial List: 13 6 8 15 16 Sum = 42, Product = 9360
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.
- Remove all the Even Digit Sum Nodes from a Circular Singly Linked List
- Sum and Product of the nodes of a Circular Singly Linked List which are divisible by K
- Sum and Product of all even digit sum Nodes of a Singly Linked List
- Splitting starting N nodes into new Circular Linked List while preserving the old nodes
- Sum of the nodes of a Circular Linked List
- Exchange first and last nodes in Circular Linked List
- Delete all the even nodes of a Circular Linked List
- Delete all odd nodes of a Circular Linked List
- Count nodes in Circular linked list
- Delete all odd or even positioned nodes from Circular Linked List
- Remove all Fibonacci Nodes from a Circular Singly Linked List
- Delete all Prime Nodes from a Circular Singly Linked List
- Remove all even parity nodes from a Doubly and Circular Singly Linked List
- Linked List Product of Nodes Between 0s
- Sum and Product of all the nodes which are less than K in the linked list
- Remove all the Even Digit Sum Nodes from a Doubly Linked List
- Find the product of last N nodes of the given Linked List
- Find the product of first k nodes of the given Linked List
- Product of the nodes of a Singly Linked List
- 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.