31. Add Two Numbers
mediumAsked at SalesforceAdd two numbers represented as linked lists (digits in reverse order). Salesforce asks this to test carry propagation and dummy-head pattern application together.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Salesforce loops.
- Glassdoor (2026-Q1)— Salesforce billing-platform team uses this exact pattern for arbitrary-precision arithmetic.
- Blind (2025-11)— Recurring on Salesforce backend onsites.
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 add
Parse each list as a number, add, build the result list.
- Time
- O(n)
- Space
- O(n)
function addTwoNumbers(l1, l2) {
const toNum = (l) => { let s = ''; while (l) { s = l.val + s; l = l.next; } return BigInt(s); };
let sum = toNum(l1) + toNum(l2);
if (sum === 0n) return { val: 0, next: null };
const dummy = { next: null };
let cur = dummy;
while (sum > 0n) { cur.next = { val: Number(sum % 10n), next: null }; cur = cur.next; sum /= 10n; }
return dummy.next;
}Tradeoff: Works with BigInt but bypasses the actual exercise. Salesforce will dock you for not doing column addition.
2. Single pass with carry and dummy head
Walk both lists in parallel; sum corresponding digits plus carry; create a new node with sum % 10; carry = sum / 10. Use a dummy head.
- Time
- O(max(m, n))
- Space
- O(max(m, n))
function addTwoNumbers(l1, l2) {
const dummy = { next: null };
let cur = dummy, carry = 0;
while (l1 || l2 || carry) {
const sum = (l1?.val || 0) + (l2?.val || 0) + carry;
carry = Math.floor(sum / 10);
cur.next = { val: sum % 10, next: null };
cur = cur.next;
l1 = l1?.next;
l2 = l2?.next;
}
return dummy.next;
}Tradeoff: Single pass with the trifecta: dummy head, optional chaining, and 'while carry' to flush the trailing carry digit. Salesforce's preferred answer.
Salesforce-specific tips
Salesforce billing-platform team uses this exact pattern for currency arithmetic across very large amounts. They grade on whether you include `carry` in the while condition (so '99 + 1' produces '100' not '00'). Bonus signal: mention that the same algorithm extends to bignum arithmetic — Salesforce's BigDecimal Apex code uses identical logic.
Common mistakes
- Forgetting carry in the loop condition — drops the final carry digit on inputs like [5] + [5].
- Not using a dummy head — special-casing the first node bloats the code.
- Computing sum % 10 and sum / 10 in wrong order or with bad division (use Math.floor).
Follow-up questions
An interviewer at Salesforce may pivot to one of these next:
- Add Two Numbers II (LC 445) — digits in forward order.
- Multiply Strings (LC 43).
- What if the lists could have negative numbers?
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why does the loop continue while carry is non-zero?
After both lists are exhausted, a leftover carry needs a new node. Example: [9] + [9] = [8, 1]. Without `|| carry`, the trailing 1 would be lost.
Why use a dummy head?
Saves a special case for the first node. With dummy, every iteration uniformly appends to cur.next.
Practice these live with InterviewChamp.AI
Drill Add Two Numbers and other Salesforce interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →