Multiply two numbers represented by Linked Lists

Given two numbers represented by linked lists, write a function that returns the multiplication of these two linked lists.

Examples:

Input : 9->4->6
8->4
Output : 79464

Input : 3->2->1
1->2
Output : 3852

Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

Solution:
Traverse both lists and generate the required numbers to be multiplied and then return the multiplied values of the two numbers.
Algorithm to generate the number from linked list representation:

1) Initialize a variable to zero
2) Start traversing the linked list
3) Add the value of first node to this variable
4) From the second node, multiply the variable by 10
first and then add the value of the node to this
variable.
5) Repeat step 4 until we reach the last node of the list.

Use the above algorithm with both of linked lists to generate the numbers.
Below is the program for multiplying two numbers represented as linked lists:

C#

Output:

First List is: 9->4->6
Second List is: 8->4
Result is: 79464