Skip to main content

2. Add Two Numbers

mediumAsked at JPMorgan

Add two non-negative integers stored as singly linked lists in reverse-digit order. JPMorgan asks this on SDE onsites as the linked-list version of the carry-propagation pattern — they specifically grade whether you can fold three loops (both lists, leftover digits, final carry) into one clean loop.

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

Source citations

Public interview reports confirming this problem appears in JPMorgan loops.

  • Glassdoor (2026-Q1)Recurring JPMorgan SDE onsite linked-list problem in 2026-Q1 reviews.
  • LeetCode (2026-Q1)Tagged JPMorgan on the LeetCode company tag page.
  • Reddit r/cscareerquestions (2025-11)JPMC SWE-I write-up: 'add two numbers — interviewer asked for the single-loop version'.

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 = [0], l2 = [0]
Output
[0]

Example 3

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. Single-loop walk with carry (optimal)

Walk both lists, summing nodes plus carry. Continue while either list has a node or the carry is non-zero. Use a sentinel head.

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

Tradeoff: One loop handles three cases (both lists active, only one active, only the trailing carry). The sentinel head removes the 'is the result empty' branch. This is the answer JPMorgan interviewers expect.

2. Convert to integer, add, convert back

Walk both lists into BigInts, add, then split the result back into reversed digits.

Time
O(n + m)
Space
O(n + m)
function addTwoNumbersBig(l1, l2) {
  function toBig(node) {
    let n = 0n;
    let place = 1n;
    while (node) {
      n += BigInt(node.val) * place;
      place *= 10n;
      node = node.next;
    }
    return n;
  }
  let sum = toBig(l1) + toBig(l2);
  const sentinel = { val: 0, next: null };
  let tail = sentinel;
  if (sum === 0n) return { val: 0, next: null };
  while (sum > 0n) {
    tail.next = { val: Number(sum % 10n), next: null };
    tail = tail.next;
    sum /= 10n;
  }
  return sentinel.next;
}

Tradeoff: Cheats around the carry logic. JPMorgan interviewers consider this disqualifying — the whole point of the question is to grade carry handling, and this dodges it. Mention it only as a one-liner sanity check after writing the canonical answer.

JPMorgan-specific tips

JPMorgan interviewers grade two things: (1) the loop continues while either list OR the carry is non-empty (most candidates forget the trailing-carry case); (2) the sentinel head removes the special case for the first result node. State both intentions before writing — narration is half the grade.

Common mistakes

  • Stopping the loop when one list ends — misses the leftover digits and the final carry.
  • Forgetting the final carry — produces wrong answers when the sum has more digits than either input.
  • Not using a sentinel head — code becomes uglier with a 'first node?' branch.
  • Computing carry as Math.floor(sum / 10) — correct but slower than sum >= 10 ? 1 : 0.

Follow-up questions

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

  • Add Two Numbers II (LC 445) — digits stored in normal order (most-significant first). Reverse, or use two stacks.
  • Multiply Two Numbers as linked lists.
  • Add Strings (LC 415) — same pattern on strings.
  • Add k linked-list numbers (extend to a k-pointer min-heap or repeated pairwise add).

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 a sentinel head here?

Without a sentinel, you have to conditionally initialise the result list on the first iteration ('is this the first node?'). The sentinel makes every iteration look the same — allocate a new node and append. The actual head is sentinel.next when you finish.

Why is the BigInt approach disqualifying?

The problem is explicitly testing carry-propagation on linked lists. The BigInt shortcut dodges the test and would not survive in environments without arbitrary-precision integers (most languages besides Python and JS BigInt). JPMorgan interviewers explicitly call this out in feedback.

Free learning resources

Curated free links for this problem.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →