Given the heads of two singly linked lists L1 and L2, where each node stores a single digit of a non-negative integer, return the product of the two numbers represented by the linked lists. Since the answer can be very large, return it modulo 10^9 + 7.
Note: The digits are stored in the same order as the number (most significant digit first).
Examples:
Input: L1 = 3 -> 2 , L2 = 2 Output: 64 Explanation: Multiplication of 32 and 2 gives 64.
[Naive Approach] Constructing the Complete Numbers - O(n + m) Time and O(1) Space
The idea is to traverse both linked lists and construct the complete numbers they represent. Once the two numbers are obtained, multiply them and return the product modulo 10^9 + 7.
Initialize two variables, num1 and num2, to store the numbers represented by the two linked lists.
Traverse the first linked list and construct num1 by appending each digit (num1 = num1 × 10 + digit).
Traverse the second linked list and construct num2 in the same manner.
Multiply the two constructed numbers.
Return the product modulo 10^9 + 7.
C++
#include<iostream>usingnamespacestd;// Structure of a linked list nodeclassNode{public:intdata;Node*next;Node(intx){data=x;next=nullptr;}};// Function to multiply two numbers represented by linked listsintmultiplyTwoLists(Node*first,Node*second){constlonglongMOD=1000000007;// Variables to store the numbers represented by the linked listslonglongnum1=0,num2=0;// Traverse the first linked list and construct the first numberwhile(first!=nullptr){num1=num1*10+first->data;first=first->next;}// Traverse the second linked list and construct the second numberwhile(second!=nullptr){num2=num2*10+second->data;second=second->next;}// Return the product modulo MOD// Note: This multiplication may overflow for very large numbers.return(num1*num2)%MOD;}intmain(){// Create first linked list: 9 -> 4 -> 6Node*first=newNode(9);first->next=newNode(4);first->next->next=newNode(6);// Create second linked list: 8 -> 4Node*second=newNode(8);second->next=newNode(4);cout<<multiplyTwoLists(first,second)<<endl;return0;}
Java
classNode{intdata;Nodenext;Node(intx){data=x;next=null;}}publicclassGFG{staticfinallongMOD=1000000007;// Function to multiply two numbers represented by// linked listsstaticlongmultiplyTwoLists(Nodefirst,Nodesecond){// Variables to store the numbers represented by the// linked listslongnum1=0,num2=0;// Traverse the first linked list and construct the// first numberwhile(first!=null){num1=num1*10+first.data;first=first.next;}// Traverse the second linked list and construct the// second numberwhile(second!=null){num2=num2*10+second.data;second=second.next;}// Return the product modulo MOD// Note: This multiplication may overflow for very// large numbers.return(num1*num2)%MOD;}publicstaticvoidmain(String[]args){// Create first linked list: 9 -> 4 -> 6Nodefirst=newNode(9);first.next=newNode(4);first.next.next=newNode(6);// Create second linked list: 8 -> 4Nodesecond=newNode(8);second.next=newNode(4);System.out.println(multiplyTwoLists(first,second));}}
Python
# Structure of a linked list nodeclassNode:def__init__(self,x):self.data=xself.next=None# Function to multiply two numbers represented by linked listsdefmultiplyTwoLists(first,second):MOD=1000000007# Variables to store the numbers represented by the linked listsnum1,num2=0,0# Traverse the first linked list and construct the first numberwhilefirst:num1=num1*10+first.datafirst=first.next# Traverse the second linked list and construct the second numberwhilesecond:num2=num2*10+second.datasecond=second.next# Return the product modulo MOD# Note: Python integers do not overflow, but this is the naive approach.return(num1*num2)%MOD# Driver codeif__name__=="__main__":# Create first linked list: 9 -> 4 -> 6first=Node(9)first.next=Node(4)first.next.next=Node(6)# Create second linked list: 8 -> 4second=Node(8)second.next=Node(4)print(multiplyTwoLists(first,second))
C#
usingSystem;classNode{publicintdata;publicNodenext;publicNode(intx){data=x;next=null;}}classGFG{staticlongMOD=1000000007;// Function to multiply two numbers represented by// linked listsstaticlongmultiplyTwoLists(Nodefirst,Nodesecond){// Variables to store the numbers represented by the// linked listslongnum1=0,num2=0;// Traverse the first linked list and construct the// first numberwhile(first!=null){num1=num1*10+first.data;first=first.next;}// Traverse the second linked list and construct the// second numberwhile(second!=null){num2=num2*10+second.data;second=second.next;}// Return the product modulo MOD// Note: This multiplication may overflow for very// large numbers.return(num1*num2)%MOD;}staticvoidMain(){// Create first linked list: 9 -> 4 -> 6Nodefirst=newNode(9);first.next=newNode(4);first.next.next=newNode(6);// Create second linked list: 8 -> 4Nodesecond=newNode(8);second.next=newNode(4);Console.WriteLine(multiplyTwoLists(first,second));}}
JavaScript
// Structure of a linked list nodeclassNode{constructor(x){this.data=x;this.next=null;}}// Function to multiply two numbers represented by linked// listsfunctionmultiplyTwoLists(first,second){constMOD=1000000007n;// Variables to store the numbers represented by the// linked listsletnum1=0n;letnum2=0n;// Traverse the first linked list and construct the// first numberwhile(first!==null){num1=num1*10n+BigInt(first.data);first=first.next;}// Traverse the second linked list and construct the// second numberwhile(second!==null){num2=num2*10n+BigInt(second.data);second=second.next;}// Return the product modulo MODreturnNumber((num1*num2)%MOD);}// Driver code// Create first linked list: 9 -> 4 -> 6letfirst=newNode(9);first.next=newNode(4);first.next.next=newNode(6);// Create second linked list: 8 -> 4letsecond=newNode(8);second.next=newNode(4);console.log(multiplyTwoLists(first,second));
Output
79464
[Expected Approach] Using Modular Arithmetic - O(n + m) Time and O(1) Space
The numbers represented by the linked lists can be extremely large, making it impossible to store them in built-in integer types without overflow. Instead of constructing the complete numbers, we compute their values modulo 10^9 + 7 while traversing the linked lists. So, while forming each number, we repeatedly take modulo to keep the intermediate values within range.
Initialize two variables num1 and num2 to 0.
Traverse the first linked list and update num1 as num1 = (num1 × 10 + digit) % MOD.
Traverse the second linked list and similarly compute num2.
Multiply num1 and num2, and take the result modulo MOD.
Return the final product.
C++
#include<iostream>usingnamespacestd;// Structure of a linked list nodeclassNode{public:intdata;Node*next;Node(intx){data=x;next=nullptr;}};staticconstintMOD=1000000007;// Function to convert a linked list into a number modulo MODlonglonggetNumber(Node*head){longlongnum=0;// Traverse the linked list and compute its value modulo MODwhile(head!=nullptr){num=(num*10+head->data)%MOD;head=head->next;}returnnum;}intmultiplyTwoLists(Node*first,Node*second){// Get the numbers represented by the linked lists modulo MODlonglongnum1=getNumber(first);longlongnum2=getNumber(second);// Return the product modulo MODreturn(num1*num2)%MOD;}intmain(){// Create first linked list: 9 -> 4 -> 6Node*first=newNode(9);first->next=newNode(4);first->next->next=newNode(6);// Create second linked list: 8 -> 4Node*second=newNode(8);second->next=newNode(4);cout<<multiplyTwoLists(first,second)<<endl;return0;}
Java
classNode{intdata;Nodenext;Node(intx){data=x;next=null;}}publicclassGFG{staticfinalintMOD=1000000007;// Function to convert a linked list into a number// modulo MODstaticlonggetNumber(Nodehead){// Variable to store the number modulo MODlongnum=0;// Traverse the linked list and compute its value// modulo MODwhile(head!=null){num=(num*10+head.data)%MOD;head=head.next;}returnnum;}// Function to multiply two numbers represented by// linked listsstaticlongmultiplyTwoLists(Nodefirst,Nodesecond){// Get the numbers represented by the linked lists// modulo MODlongnum1=getNumber(first);longnum2=getNumber(second);// Return the product modulo MODreturn(num1*num2)%MOD;}publicstaticvoidmain(String[]args){Nodefirst=newNode(9);first.next=newNode(4);first.next.next=newNode(6);Nodesecond=newNode(8);second.next=newNode(4);System.out.println(multiplyTwoLists(first,second));}}
Python
classNode:def__init__(self,x):self.data=xself.next=None# Function to convert a linked list into a number modulo MODdefgetNumber(head,MOD):# Variable to store the number modulo MODnum=0# Traverse the linked list and compute its value modulo MODwhilehead:num=(num*10+head.data)%MODhead=head.nextreturnnum# Function to multiply two numbers represented by linked listsdefmultiplyTwoLists(first,second):MOD=1000000007# Get the numbers represented by the linked lists modulo MODnum1=getNumber(first,MOD)num2=getNumber(second,MOD)# Return the product modulo MODreturn(num1*num2)%MOD# Driver Codeif__name__=="__main__":first=Node(9)first.next=Node(4)first.next.next=Node(6)second=Node(8)second.next=Node(4)print(multiplyTwoLists(first,second))
C#
usingSystem;classNode{publicintdata;publicNodenext;publicNode(intx){data=x;next=null;}}classGFG{staticlongMOD=1000000007;// Function to convert a linked list into a number// modulo MODstaticlongGetNumber(Nodehead){// Variable to store the number modulo MODlongnum=0;// Traverse the linked list and compute its value// modulo MODwhile(head!=null){num=(num*10+head.data)%MOD;head=head.next;}returnnum;}// Function to multiply two numbers represented by// linked listsstaticlongmultiplyTwoLists(Nodefirst,Nodesecond){// Get the numbers represented by the linked lists// modulo MODlongnum1=GetNumber(first);longnum2=GetNumber(second);// Return the product modulo MODreturn(num1*num2)%MOD;}staticvoidMain(){Nodefirst=newNode(9);first.next=newNode(4);first.next.next=newNode(6);Nodesecond=newNode(8);second.next=newNode(4);Console.WriteLine(multiplyTwoLists(first,second));}}
JavaScript
classNode{constructor(x){this.data=x;this.next=null;}}// Function to convert a linked list into a number modulo// MODfunctiongetNumber(head,MOD){// Variable to store the number modulo MODletnum=0n;// Traverse the linked list and compute its value modulo// MODwhile(head!==null){num=(num*10n+BigInt(head.data))%MOD;head=head.next;}returnnum;}// Function to multiply two numbers represented by linked// listsfunctionmultiplyTwoLists(first,second){constMOD=1000000007n;// Get the numbers represented by the linked lists// modulo MODconstnum1=getNumber(first,MOD);constnum2=getNumber(second,MOD);// Return the product modulo MODreturnNumber((num1*num2)%MOD);}// Driver Codeletfirst=newNode(9);first.next=newNode(4);first.next.next=newNode(6);letsecond=newNode(8);second.next=newNode(4);console.log(multiplyTwoLists(first,second));