31. Add Two Numbers
mediumAsked at RedditAdd two big integers represented as linked lists (least-significant-digit first). Reddit asks this to test linked-list arithmetic with carry — the same kind of pointer-walking they use to merge cross-shard counter chunks.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Reddit loops.
- Glassdoor (2026-Q1)— Reddit on-site question for backend / infra roles.
- Blind (2025-11)— Reported in Reddit comments-team rounds.
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 <= 9It is guaranteed that the list represents a number that does not have leading zeros.
Examples
Example 1
l1 = [2,4,3], l2 = [5,6,4][7,0,8]Explanation: 342 + 465 = 807.
Example 2
l1 = [0], l2 = [0][0]Example 3
l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9][8,9,9,9,0,0,0,1]Approaches
1. Convert to numbers and back
Build the integers, add, then convert back to linked list.
- 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);
const dummy = { next: null }; let cur = dummy;
if (sum === 0n) return { val: 0, next: null };
while (sum > 0n) { cur.next = { val: Number(sum % 10n), next: null }; cur = cur.next; sum /= 10n; }
return dummy.next;
}Tradeoff: Requires BigInt for big lists. Defeats the spirit of the problem.
2. Single-pass with carry (optimal)
Walk both lists simultaneously. At each step: digit sum + carry. New digit = sum % 10, new carry = sum / 10.
- Time
- O(max(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: Linear time, single pass. The 'while (l1 || l2 || carry)' is the critical loop guard.
Reddit-specific tips
Reddit interviewers care about the carry-at-the-end case ([9,9,9] + [1] = [0,0,0,1]). Bonus signal: discuss what happens if the lists are stored most-significant-first (variant LC 445) — you'd reverse or use a stack.
Common mistakes
- Forgetting carry in the loop condition (drops final carry).
- Using parseInt — overflows for long lists.
- Not advancing l1/l2 when one is null.
Follow-up questions
An interviewer at Reddit may pivot to one of these next:
- Add Two Numbers II (LC 445) — most-significant-first.
- What if the lists could be negative? Add a sign field.
- Multiply two big integers represented as linked lists.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why a dummy head?
Cleaner than tracking 'first iteration'. The tail always exists.
Why store in reverse order?
Reverse order makes carry propagation work without an extra pass — you start at the least significant digit.
Practice these live with InterviewChamp.AI
Drill Add Two Numbers and other Reddit interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →