# Product of the alternate nodes of linked list

• Difficulty Level : Basic
• Last Updated : 16 Nov, 2021

Given a linked list, the task is to print the product of alternate nodes of the given linked list.

Examples

Input : 1 -> 8 -> 3 -> 10 -> 17 -> 22 -> 29 -> 42
Output : 1479
Alternate nodes : 1 -> 3 -> 17 -> 29

Input : 10 -> 17 -> 33 -> 38 -> 73
Output : 24090
Alternate nodes : 10 -> 33 -> 73

Iterative Approach:

1. Traverse the whole linked list.
2. Set prod = 1 and count=0.
3. Multiply the data of the node with the prod when the count is even.
4. Visit the next node.

Below is the implementation of this approach:

## Javascript


Output:
1056

Time Complexity: O(N)
Auxiliary Space: O(1)

Recursive Approach

1. Initialize a static variable(say flag).
2. If the flag is odd, multiply the node with the product.
3. Increase head and flag by 1, and recurse for next nodes.

Below is the implementation of this approach:

## Javascript


Output:
1056

Time Complexity: O(N)
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up