Given two numbers represented by linked lists, write a function that returns the head of the new linked list that represents the number that is the product of those numbers.
Examples:
Input : 9->4->6
8->4
Output : 7->9->4->6->4
Input : 9->9->9->4->6->9
9->9->8->4->9
Output : 9->9->7->9->5->9->8->0->1->8->1
We have already discussed a solution in below post.
Multiply two numbers represented by Linked Lists
The solution discussed above store result in an integer. Her we store result in a third list so that large numbers can be handled.
Remember old school multiplication? we imitate that process. On paper, we take the last digit of a number and multiply with the second number and write the product. Now leave the last column and same way each digit of one number is multiplied with every digit of other number and every time result is written by leaving one last column. then add these columns that forms the number. Now assume these columns as nodes of the resultant linked list. We make resultant linked list in reversed fashion.
Algorithm
Reverse both linked lists
Make a linked list of maximum result size (m + n + 1)
For each node of one list
For each node of second list
a) Multiply nodes
b) Add digit in result LL at corresponding
position
c) Now resultant node itself can be higher
than one digit
d) Make carry for next node
Leave one last column means next time start
From next node in result list
Reverse the resulted linked list
// C program to Multiply two numbers
// represented as linked lists
#include <stdio.h>
#include <stdlib.h>
// Linked list Node
struct Node {
int data;
struct Node* next;
};
// Function to create a new Node
// with given data
struct Node* newNode(int data)
{
struct Node* new_node =
(struct Node*)malloc(sizeof(struct Node));
new_node->data = data;
new_node->next = NULL;
return new_node;
}
// Function to insert a Node at the
// beginning of the Linked List
void push(struct Node** head_ref, int new_data)
{
// allocate Node
struct Node* new_node = newNode(new_data);
// link the old list off the new Node
new_node->next = (*head_ref);
// move the head to point to the new Node
(*head_ref) = new_node;
}
// Function to reverse the linked list and return
// its length
int reverse(struct Node** head_ref)
{
struct Node* prev = NULL;
struct Node* current = *head_ref;
struct Node* next;
int len = 0;
while (current != NULL) {
len++;
next = current->next;
current->next = prev;
prev = current;
current = next;
}
*head_ref = prev;
return len;
}
// Function to make an empty linked list of
// given size
struct Node* make_empty_list(int size)
{
struct Node* head = NULL;
while (size--)
push(&head, 0);
return head;
}
// Multiply contents of two linked lists => store
// in another list and return its head
struct Node* multiplyTwoLists(struct Node* first,
struct Node* second)
{
// reverse the lists to muliply from end
// m and n lengths of linked lists to make
// and empty list
int m = reverse(&first), n = reverse(&second);
// make a list that will contain the result
// of multiplication.
// m+n+1 can be max size of the list
struct Node* result = make_empty_list(m + n + 1);
// pointers for traverse linked lists and also
// to reverse them after
struct Node *second_ptr = second,
*result_ptr1 = result, *result_ptr2, *first_ptr;
// multiply each Node of second list with first
while (second_ptr) {
int carry = 0;
// each time we start from the next of Node
// from which we started last time
result_ptr2 = result_ptr1;
first_ptr = first;
while (first_ptr) {
// multiply a first list's digit with a
// current second list's digit
int mul = first_ptr->data * second_ptr->data
+ carry;
// Assigne the product to corresponding Node
// of result
result_ptr2->data += mul % 10;
// now resultant Node itself can have more
// than 1 digit
carry = mul / 10 + result_ptr2->data / 10;
result_ptr2->data = result_ptr2->data % 10;
first_ptr = first_ptr->next;
result_ptr2 = result_ptr2->next;
}
// if carry is remaining from last multiplication
if (carry > 0) {
result_ptr2->data += carry;
}
result_ptr1 = result_ptr1->next;
second_ptr = second_ptr->next;
}
// reverse the result_list as it was populated
// from last Node
reverse(&result);
reverse(&first);
reverse(&second);
// remove if there are zeros at starting
while (result->data == 0) {
struct Node* temp = result;
result = result->next;
free(temp);
}
// Return head of multiplication list
return result;
}
// A utility function to print a linked list
void printList(struct Node* Node)
{
while (Node != NULL) {
printf("%d", Node->data);
if (Node->next)
printf("->");
Node = Node->next;
}
printf("\n");
}
// Driver program to test above function
int main(void)
{
struct Node* first = NULL;
struct Node* second = NULL;
// create first list 9->9->9->4->6->9
push(&first, 9);
push(&first, 6);
push(&first, 4);
push(&first, 9);
push(&first, 9);
push(&first, 9);
printf("First List is: ");
printList(first);
// create second list 9->9->8->4->9
push(&second, 9);
push(&second, 4);
push(&second, 8);
push(&second, 9);
push(&second, 9);
printf("Second List is: ");
printList(second);
// Multiply the two lists and see result
struct Node* result = multiplyTwoLists(first, second);
printf("Resultant list is: ");
printList(result);
return 0;
}
Output:
First List is: 9->9->9->4->6->9 Second List is: 9->9->8->4->9 Resultant list is: 9->9->7->9->5->9->8->0->1->8->1
Note: we can take care of resultant node that can have more than 1 digit outside the loop just traverse the result list and add carry to next digit before reversing.
This article is contributed by Shubham_Raghuwanshi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geekforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

