Sum and Product of nodes with value as even digit sum in Circular Linked List

• Difficulty Level : Basic
• Last Updated : 15 Jul, 2021

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.

Examples:

Input: List = 15 -> 16 -> 8 -> 6 -> 13
Output: Sum = 42, Product = 9360
Explanation:
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
Explanation:
The list contains 2 Even Digit Sum data values 4 and 2.
Sum = 4 + 2 = 6
Product = 4 * 2 = 8

Approach:

1. 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.
2. Start traversing the linked list using a do-while loop until all the nodes get traversed.
3. 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.
4. Print the sum and product.

Below is the implementation of the above approach:

Javascript



Output:

Initial List: 13 6 8 15 16
Sum = 42, Product = 9360

My Personal Notes arrow_drop_up