Multiply 2 no. represented by LL
Pattern: Traversal and dummy nodes
Read each list as a number, digit by digit from the head, keeping it modulo 1,000,000,007 so it never overflows. Multiply the two remainders and take the remainder again.
No verified practice link yet. The guide below states the problem in full, so you can still solve it here.
The problem
Two whole numbers are stored in linked lists, one digit per node, with the most significant digit at the head. Return
their product modulo 1,000,000,007. For example, 9 → 4 → 6 and 8 → 4 give 946 × 84 = 79464. The lists can be
long, so the numbers themselves may not fit in any number type.
This problem is from Love Babbar's 450 sheet, and this guide has no verified link for it yet. The function name
multiplyTwoLists and the modulo rule (as on GeeksforGeeks' version of this problem) are this guide's own.
Think first
- How do you turn digits, read left to right, into a number?
- The numbers can have 100 digits. Why does that matter?
- Which property of remainders lets you multiply without the full numbers?
Hints
Brute force, and why it fails
Build both numbers exactly and multiply: needs big-number arithmetic in Java and C++. Keeping remainders gives the answer with plain 64-bit numbers in O(m + n).
The key insight
The question only asks for the product's remainder. Remainders survive addition and multiplication, so the huge numbers never need to exist.
Dry run
| first | 946 |
|---|
first is 9 → 4 → 6 and second is 8 → 4. Read first left to right. v = 0, then 9, then 94, then 946.
The plan
M = 1000000007
value(list): v = 0; for each digit d from the head: v = (v * 10 + d) mod M; return v
return value(first) * value(second) mod M
Complexity
- Time
O(m + n)- Space
O(1)
One walk over each list.
Edge cases
- A zero, as in
0and5: 0. - Leading zeros, as in
0 → 1 → 2: the value is 12, which the formula handles. - Long lists: never build the full number.
I've built my logic, show the code
Java
class Solution {
public long multiplyTwoLists(ListNode first, ListNode second) {
final long MOD = 1_000_000_007L;
return value(first, MOD) * value(second, MOD) % MOD;
}
private long value(ListNode head, long mod) {
long v = 0;
for (ListNode cur = head; cur != null; cur = cur.next) v = (v * 10 + cur.val) % mod; // read digits left to right
return v;
}
}C++
class Solution {
public:
long long multiplyTwoLists(ListNode* first, ListNode* second) {
const long long MOD = 1000000007LL;
return value(first, MOD) * value(second, MOD) % MOD;
}
long long value(ListNode* head, long long mod) {
long long v = 0;
for (ListNode* cur = head; cur != NULL; cur = cur->next) v = (v * 10 + cur->val) % mod; // digits left to right
return v;
}
};Python
from lib import ListNode
class Solution:
def multiplyTwoLists(self, first: ListNode, second: ListNode) -> int:
MOD = 1_000_000_007
return self.value(first, MOD) * self.value(second, MOD) % MOD
def value(self, head: ListNode, mod: int) -> int:
v, cur = 0, head
while cur is not None: # read digits left to right
v = (v * 10 + cur.val) % mod
cur = cur.next
return vCommon mistakes
- Multiplying in 32-bit numbers, which overflows.
- Taking the remainder only at the end, after the number has already overflowed.
- Reading the digits from the tail, which reverses the number.