To multiply two numbers represented by linked lists, you need to traverse each linked list from the head node to the end. For each linked list, initialize a variable to zero, which will be used to store the numerical value of the linked list. By processing each node, start by adding the value of the first node to this variable. For each subsequent node, multiply the variable by 10 and then add the node's value to it. This approach effectively reconstructs the number represented by the linked list. To handle potential overflow and ensure that the computations remain within manageable bounds, use the modulo operation with 1e9 + 7 after each arithmetic operation. This will keep the intermediate results within a safe range and prevent overflow during multiplication.
Below is the implementation of the above approach :
C++
// C++ program to Multiply two numbers // represented as linked lists#include<iostream>usingnamespacestd;longlongMOD=1000000007;classNode{public:intdata;Node*next;Node(intx){data=x;next=nullptr;}};// Multiply contents of two linked listslonglongmultiplyTwoLists(Node*first,Node*second){longlongnum1=0,num2=0;// Traverse the first list and // construct the number with modulowhile(first!=nullptr){num1=(num1*10+first->data)%MOD;first=first->next;}// Traverse the second list and // construct the number with modulowhile(second!=nullptr){num2=(num2*10+second->data)%MOD;second=second->next;}// Return the product of the // two numbers with moduloreturn(num1*num2)%MOD;}voidprintList(Node*curr){while(curr!=nullptr){cout<<curr->data<<" ";curr=curr->next;}}intmain(){// Create first list 9->4->6Node*head1=newNode(9);head1->next=newNode(4);head1->next->next=newNode(6);// Create second list 8->4Node*head2=newNode(8);head2->next=newNode(4);cout<<multiplyTwoLists(head1,head2)<<endl;return0;}
C
// C program to Multiply two numbers// represented as linked lists#include<stdlib.h>#define MOD 1000000007structNode{intdata;structNode*next;};// Multiply contents of two linked listslonglongmultiplyTwoLists(structNode*first,structNode*second){longlongnum1=0,num2=0;// Traverse the first list and// construct the number with modulowhile(first!=NULL){num1=(num1*10+first->data)%MOD;first=first->next;}// Traverse the second list and// construct the number with modulowhile(second!=NULL){num2=(num2*10+second->data)%MOD;second=second->next;}// Return the product of the two // numbers with moduloreturn(num1*num2)%MOD;}voidprintList(structNode*curr){while(curr!=NULL){printf("%d ",curr->data);curr=curr->next;}}structNode*createNode(intdata){structNode*newNode=(structNode*)malloc(sizeof(structNode));newNode->data=data;newNode->next=NULL;returnnewNode;}intmain(){// Create first list 9->4->6structNode*head1=createNode(9);head1->next=createNode(4);head1->next->next=createNode(6);// Create second list 8->4structNode*head2=createNode(8);head2->next=createNode(4);printf("%lld\n",multiplyTwoLists(head1,head2));return0;}
Java
// Java program to Multiply two numbers// represented as linked listsclassNode{intdata;Nodenext;Node(intdata){this.data=data;this.next=null;}}publicclassGfG{// Multiply contents of two linked listsstaticlongmultiplyTwoLists(Nodefirst,Nodesecond){longMOD=1000000007;longnum1=0,num2=0;// Traverse the first list and // construct the number with modulowhile(first!=null||second!=null){if(first!=null){num1=((num1*10)+first.data)%MOD;first=first.next;}// Traverse the second list and // construct the number with moduloif(second!=null){num2=((num2*10)+second.data)%MOD;second=second.next;}}// Return the product of the two // numbers with moduloreturn(num1*num2)%MOD;}staticvoidprintList(Nodecurr){while(curr!=null){System.out.print(curr.data+" ");curr=curr.next;}}publicstaticvoidmain(Stringargs[]){// create first list 9->4->6Nodehead1=newNode(9);head1.next=newNode(4);head1.next.next=newNode(6);// create second list 8->4Nodehead2=newNode(8);head2.next=newNode(4);System.out.println(multiplyTwoLists(head1,head2));}}
Python
# Python3 to multiply two numbers# represented as Linked ListsMOD=1000000007classNode:def__init__(self,data):self.data=dataself.next=None# Multiply contents of two linked listsdefmultiplyTwoLists(first,second):num1=0num2=0# Traverse the first list and # construct the number with modulowhilefirstisnotNone:num1=(num1*10+first.data)%MODfirst=first.next# Traverse the second list and # construct the number with modulowhilesecondisnotNone:num2=(num2*10+second.data)%MODsecond=second.next# Return the product of the two# numbers with moduloreturn(num1*num2)%MODdefprintList(curr):whilecurrisnotNone:print(curr.data,end=" ")curr=curr.nextif__name__=="__main__":# Create first list 9->4->6head1=Node(9)head1.next=Node(4)head1.next.next=Node(6)# Create second list 8->4head2=Node(8)head2.next=Node(4)print(multiplyTwoLists(head1,head2))
C#
// C++ program to Multiply two numbers// represented as linked listsusingSystem;publicclassNode{publicintdata;publicNodenext;publicNode(intdata){this.data=data;this.next=null;}}classGfG{// Multiply contents of two linked listsstaticlongmultiply(Nodefirst,Nodesecond){varMOD=1000000007;varnum1=0;varnum2=0;// Traverse the first list and// construct the number with modulowhile(first!=null||second!=null){if(first!=null){num1=((num1)*10)%MOD+first.data;first=first.next;}if(second!=null){num2=((num2)*10)%MOD+second.data;second=second.next;}}// Return the product of the two // numbers with modulreturn((num1%MOD)*(num2%MOD))%MOD;}staticvoidprintList(Nodecurr){while(curr!=null){Console.Write(curr.data+" ");curr=curr.next;}}staticvoidMain(String[]args){// create first list 9->4->6varfirst=newNode(9);first.next=newNode(4);first.next.next=newNode(6);// create second list 8->4varsecond=newNode(8);second.next=newNode(4);Console.WriteLine(multiply(first,second));}}
JavaScript
// JavaScript program to Multiply two numbers// represented as linked listsconstMOD=1000000007;classNode{constructor(data){this.data=data;this.next=null;}}// Multiply contents of two linked listsfunctionmultiplyTwoLists(first,second){letnum1=0,num2=0;// Traverse the first list and // construct the number with modulowhile(first!==null){num1=(num1*10+first.data)%MOD;first=first.next;}// Traverse the second list and // construct the number with modulowhile(second!==null){num2=(num2*10+second.data)%MOD;second=second.next;}// Return the product of the two // numbers with moduloreturn(num1*num2)%MOD;}functionprintList(curr){while(curr!==null){console.log(curr.data+" ");curr=curr.next;}console.log();}// Create first list 9->4->6lethead1=newNode(9);head1.next=newNode(4);head1.next.next=newNode(6);// Create second list 8->4lethead2=newNode(8);head2.next=newNode(4);console.log(multiplyTwoLists(head1,head2));
Output
79464
Time Complexity: O(max(n1, n2)), where n1 and n2 represents the number of nodes present in the first and second linked list respectively. Auxiliary Space: O(1)