Skip to main content

11. Add Two Numbers

mediumAsked at ByteDance

Add two non-negative integers stored as reverse-order linked lists — ByteDance uses it to test edge-case pointer handling before deeper feed-pipeline questions.

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

Problem

Given two non-empty linked lists representing non-negative integers in reverse order, where each node holds a single digit, return the sum as a linked list in the same reverse order.

Constraints

  • 1 <= each list length <= 100
  • 0 <= Node.val <= 9
  • No leading zeros except the number 0 itself

Examples

Example 1

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

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 bigint and back

Walk both lists into strings, sum as BigInt, then rebuild a list.

Time
O(n)
Space
O(n)
// build numbers from digits, sum, then walk digits back into nodes

Tradeoff:

2. Simulate column addition with carry

Walk both lists in lockstep summing digits plus a carry, producing one node per column. Handles unequal lengths naturally.

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

Tradeoff:

ByteDance-specific tips

ByteDance interviewers expect the trailing-carry case called out before code, matching how their feed-aggregation team plans overflow handling up front.

Solve it now

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

Output

Press Run or Cmd+Enter to execute

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →