Multiply two linked lists

PROBLEM :

The task is to complete the function  multiplyTwoLists which multiplies two linked lists l1 and l2 and returns their product . The function takes two linked list l1, l2 as its arguments.

Note: The output could be large take modulo 10^9+7.

Input:
The first line of input contains an integer T denoting the no 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.

Constraints:
1<=T<=100
1<=n,m<=100

Example(To be used only for expected output):
Input
2
2
3 2
1
2
3
1 0 0
2
1 0
Output
64
1000

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

/*The method multiplies
two  linked lists l1 and l2
and returns their product*/

/*You are required to complete this method*/

#define MOD 1000000007 ;
long long convert_in_number(node *) ;
long long  multiplyTwoLists (node* l1, node* l2)
{
    if(l1==NULL&&l2==NULL)
        return 0 ;
       
    long long no1,no2 ;
   
    no1=convert_in_number(l1) ;
    no2=convert_in_number(l2) ;
   
    return (no1*no2)%MOD ;
}

long long convert_in_number(node *head)
{
    long long num ;
    num=0 ;
   
    while(head!=NULL)
    {
        num=num*10+head->data ;
        num=num%MOD ;
        head=head->next ;
    }
    return num ;
}

---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )