Skip to main content

31. Add Two Numbers

mediumAsked at Workday

Given two numbers as reversed linked lists, add them and return as a linked list. Workday uses this to test carry-propagation discipline — same as adding two arbitrary-precision payroll totals stored as digit lists.

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

Source citations

Public interview reports confirming this problem appears in Workday loops.

  • Glassdoor (2025-Q4)Workday SDE2 phone screen.
  • Blind (2026)Workday Pleasanton finance-platform team.

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 integers, add, convert back

Walk both lists to extract digits, sum as numbers, rebuild list.

Time
O(n + m)
Space
O(n + m)
// Number(...) overflows past 15 digits — bug in disguise.

Tradeoff: Defeats the point. Inputs can be 100 digits; JS number maxes at 15.

2. Digit-by-digit with carry

Walk both lists in lockstep. Sum + carry; emit digit; propagate carry. Dummy head simplifies the result-list logic.

Time
O(n + m)
Space
O(max(n, m))
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;
    carry = Math.floor(sum / 10);
    tail.next = { val: sum % 10, next: null };
    tail = tail.next;
    if (l1) l1 = l1.next;
    if (l2) l2 = l2.next;
  }
  return dummy.next;
}

Tradeoff: Single pass. The 'or carry' in the loop guard handles the [9,9,9] + [1] = [0,0,0,1] case.

Workday-specific tips

Workday grades the carry-after-last-digit case ([9,9] + [1] = [0,0,1]). Without the 'or carry' guard, you'd return [0,0]. Walk through this case before coding. Also: don't convert to Number — bigint overflow is a real bug.

Common mistakes

  • Forgetting the 'or carry' in the loop guard.
  • Using Number(list) — overflows past 15 digits.
  • Treating one list as longer and ignoring the shorter's null.

Follow-up questions

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

  • Add Two Numbers II (LC 445) — digits in forward order (use stacks or reverse).
  • Multiply Strings (LC 43).
  • Plus One Linked List (LC 369).

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 the dummy head?

Avoids special-casing the first node. The result starts at dummy.next.

What if the lists were forward-order?

Reverse both first (or push to a stack and pop). Adding right-to-left is the natural order for carries.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →