Skip to main content

31. Add Two Numbers

mediumAsked at Vercel

Add two numbers represented as linked lists with digits in reverse order. Vercel asks this for the carry-propagation pattern across two cursors — the same shape they use when merging deltas from two edge nodes that drifted out of sync.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Vercel loops.

  • Glassdoor (2025-Q4)Vercel platform onsite first problem.
  • LeetCode Discuss (2026-Q1)Listed in Vercel interview pool.

Problem

You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list. You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Constraints

  • The number of nodes in each linked list is in the range [1, 100].
  • 0 <= Node.val <= 9
  • It is guaranteed that the list represents a number that does not have leading zeros.

Examples

Example 1

Input
l1 = [2,4,3], l2 = [5,6,4]
Output
[7,0,8]

Explanation: 342 + 465 = 807.

Example 2

Input
l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
Output
[8,9,9,9,0,0,0,1]

Approaches

1. Convert to numbers, add, rebuild

Walk both lists, convert to BigInt, sum, split into digits.

Time
O(n)
Space
O(n)
function addTwoNumbers(l1, l2) {
  const toNum = (l) => {
    let n = 0n, p = 1n;
    while (l) { n += BigInt(l.val) * p; p *= 10n; l = l.next; }
    return n;
  };
  let sum = toNum(l1) + toNum(l2);
  if (sum === 0n) return { val: 0, next: null };
  const dummy = { val: 0, next: null };
  let tail = dummy;
  while (sum > 0n) {
    tail.next = { val: Number(sum % 10n), next: null };
    tail = tail.next;
    sum /= 10n;
  }
  return dummy.next;
}

Tradeoff: Side-steps the algorithm. Vercel will accept it only if you flag the trade-off, then offer the carry version.

2. Walk both, propagate carry (optimal)

Iterate with one pointer per list plus a carry. At each step, compute sum = l1.val + l2.val + carry; create a node with sum % 10; advance carry = floor(sum / 10).

Time
O(max(m,n))
Space
O(max(m,n)) output
function addTwoNumbers(l1, l2) {
  const dummy = { val: 0, next: null };
  let tail = dummy;
  let carry = 0;
  while (l1 || l2 || carry) {
    const a = l1 ? l1.val : 0;
    const b = l2 ? l2.val : 0;
    const sum = a + b + carry;
    tail.next = { val: sum % 10, next: null };
    tail = tail.next;
    carry = Math.floor(sum / 10);
    if (l1) l1 = l1.next;
    if (l2) l2 = l2.next;
  }
  return dummy.next;
}

Tradeoff: Single pass, handles different-length inputs and the final carry naturally. The `|| carry` in the loop guard handles the all-9s overflow case (999 + 1 = 1000).

Vercel-specific tips

Vercel grades the carry-handling loop. Bonus signal: the `|| carry` in the loop guard (catches the overflow digit when both lists end but carry is 1), and using a dummy head to simplify the first-node case. Articulate the digit-order convention (reverse means least-significant first, which is GOOD because carries naturally propagate forward).

Common mistakes

  • Looping while (l1 && l2) — drops the longer list's tail.
  • Forgetting to handle the final carry — produces 99 + 1 = 00 instead of 001.
  • Returning dummy instead of dummy.next.

Follow-up questions

An interviewer at Vercel may pivot to one of these next:

  • Add Two Numbers II (LC 445) — digits in forward order; reverse first or use a stack.
  • Plus One (LC 66) — same carry pattern on an array.
  • Multiply Strings (LC 43) — generalized carry + shift.

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why reverse order?

Least-significant digit first means carries propagate FORWARD as you walk, mirroring how you add by hand. Forward-order input requires reversal or a stack.

Why use a dummy head?

It removes the 'is this the first node' branch on every iteration. You always `tail.next = newNode` and advance — clean and bug-free.

Practice these live with InterviewChamp.AI

Drill Add Two Numbers and other Vercel interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →