Multiply two linked lists | Practice | GeeksforGeeks

Given elements as nodes of the two linked lists. The task is to multiply these two linked lists, say L1 and L2. 
Note: The output could be large take modulo 109+7.
Input:
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow, the first line of each test case contains an integer N denoting the size of the first linked list (L1). In the next line are the space separated values of the first linked list. The third line of each test case contains an integer M denoting the size of the second linked list (L2). In the forth line are space separated values of the second linked list.
Output:
For each test case output will be an integer denoting the product of the two linked list.
User Task:
The task is to complete the function multiplyTwoLists() which should multiply the given two linked lists and return the result.
Constraints:
1 <= T <= 100
1 <= N, M <= 100
Example:
Input:
2
2
3 2
1
2
3
1 0 0
2
1 0 
Output:
64
1000
Explanation:
Testcase 1: 32*2 = 64.
Testcase 2: 100*10 = 1000.
 
0
tthakare735 days ago
//java Solution -> TC -> 0.3
//hint ->
 class GfG{
   public long multiplyTwoLists(Node l1,Node l2){
          //add code here.
        long num1 = 0, num2 = 0, mod = 1000000007;
        
        while(l1 != null){
            num1 = ((num1 * 10) + l1.data)%mod;
            l1 = l1.next;
        }
        
        while(l2 != null){
            num2 = ((num2 * 10) + l2.data)%mod;
            l2 = l2.next;
        }
        
        return (num1*num2)%mod;
   }
}
+1
shahabuddinbravo405 days ago
long long  multiplyTwoLists (Node* l1, Node* l2){  long long mod=1000000007;    Node *ptr1=l1,*ptr2=l2;    long long first=0,second=0;    while(ptr1)  {      first=(first*10)%mod+ptr1->data;      ptr1=ptr1->next;  }    while(ptr2)  {      second=(second*10)%mod+ptr2->data;      ptr2=ptr2->next;  }    long long ans=((first%mod)*(second%mod))%mod;    return ans;}
0
badgujarsachin832 weeks ago
long long  multiplyTwoLists (Node* l1, Node* l2)
{
  //Your code her
  long long mod=1000000007;
  Node* temp1=l1;;
  Node*temp2=l2;
  long long a=0,b=0;
  while(temp1!=NULL){
      a=((a)*10)%mod+temp1->data;
      temp1=temp1->next;
  }
  while(temp2!=NULL){
      b=((b)*10)%mod+temp2->data;
      temp2=temp2->next;
  }
  return ((a%mod)*(b%mod)%mod);
}
-1
19bcs40691 month ago
long long  multiplyTwoLists (Node* l1, Node* l2){   long long num1=0;   long long num2=0;   Node *a=l1;   Node *b=l2;   while(a){       num1=(num1*10+a->data)%1000000007;       a=a->next;   }   while(b){       num2=(num2*10+b->data)%1000000007;       b=b->next;   }   return (num1*num2)%1000000007; //Your code here}
0
mishra07adi2 months ago
Java Solution
class GfG{
  /*You are required to complete this method */
  
    public long multiplyTwoLists(Node l1,Node l2)
    {
        //add code here.
        
        long N = 1000000007;
        long num1 = 0, num2 = 0;
        
        while(l1!=null || l2!=null)
        {
            if(l1 != null)
            {
                num1 = ((num1)*10)%N + l1.data;
                l1=l1.next;
            }
            
            if(l2 != null)
            {
                num2 = ((num2)*10)%N + l2.data;
                l2=l2.next;
            }
        }
        
        return ((num1%N)*(num2%N)%N);
    }
}
0
riteshnaik443 months ago
#define p 1000000007

long long  multiplyTwoLists (Node* l1, Node* l2)
{
    Node* curr1=l1;
    Node* curr2=l2;
    long long num1=0;
    long long num2=0;
    
    while(curr1!=NULL){
        num1=( num1*10 + curr1->data ) % p;
        curr1=curr1->next;
    }
    
    while(curr2!=NULL){
        num2=( num2*10 + curr2->data ) % p;
        curr2=curr2->next;
    }
    long long ans=(num1*num2) % p;
    
    
    return ans;
    
    
    
}
0
codewithmitesh3 months ago
 
+5
manishpritmani063 months ago
long long  multiplyTwoLists (Node* l1, Node* l2)
{
  long long num1 = 0;
  long long num2 = 0;
  while(l1!=nullptr){
      num1 = (num1*10 + l1->data)%1000000007;
      l1 = l1->next;
  }
  while(l2!=nullptr){
      num2 = (num2*10 + l2->data)%1000000007;
      l2 = l2->next;
  }
  return (num1*num2)%1000000007;
}
0
hemantsoni3 months ago
0
sajalgupta2433 months ago
#define g 1000000007long long  multiplyTwoLists (Node* l1, Node* l2){ //Your code here   long long int r1 = 0;   while(l1 != NULL){       r1 = (r1*10 + l1->data)%g;       l1 = l1->next;   }      long long int r2 = 0;      while(l2 != NULL){       r2 = (r2*10 + l2->data)%g;       l2 = l2->next;   }   return (r1*r2)%g;}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.