shashi
2 min readOct 2, 2024

Multiply two linked lists

Multiply two linked lists

Difficulty: EasyAccuracy: 46.33%Submissions: 83K+Points: 2

Given elements as nodes of the two singly 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.

Examples :

Input: LinkedList L1 : 3->2 , LinkedList L2 : 2
Output: 64
Explanation:
Multiplication of 32 and 2 gives 64.
Input: LinkedList L1: 1->0->0 , LinkedList L2 : 1->0
Output: 1000
Explanation:
Multiplication of 100 and 10 gives 1000.

Expected Time Complexity: O(max(n,m))
Expected Auxilliary Space: O(1)
where n is the size of L1 and m is the size of L2

Constraints:
1 <= number of nodes <= 9
0 <= node->data <= 9

Try more examples



class Solution {
public long multiplyTwoLists(Node first, Node second) {
// Code here
return (getNumber( first) * getNumber(second) ) % 1000000007;
}

public Long getNumber(Node head){
Node temp= head;
long num = 0L ;
while (temp!=null) { num= num*10 + temp.data; temp=temp.next; }
return num;
}
}
shashi
shashi

No responses yet