Multiply two numbers represented by Linked Lists Easy 0 attempts
GeeksforGeeks ↗

Multiply two numbers represented by Linked Lists

Easy Linked List GeeksforGeeks

Given two numbers represented by linked lists, multiply them and return the result.

Example: l1 = [3,2], l2 = [2] → 32 × 2 = 64

Sample Input
Sample Output
Constraints
  • 1 to 100 nodes per list
  • 0 <= Node.val <= 9
Topics

Convert to Numbers

function multiplyLists(l1, l2) {
  let n1 = 0n, n2 = 0n;
  while (l1) { n1 = n1 * 10n + BigInt(l1.val); l1 = l1.next; }
  while (l2) { n2 = n2 * 10n + BigInt(l2.val); l2 = l2.next; }
  return Number((n1 * n2) % 1000000007n);
}

Time: O(m + n) | Space: O(1)

Saved in this browser only. Private to you.

JavaScript