Skip to main content

17. Add Two Numbers

mediumAsked at PayPal

Add two non-negative integers represented as reversed linked lists and return the sum as a linked list. PayPal favors this problem because carry propagation mirrors real-world multi-precision arithmetic in payment processing systems.

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

Source citations

Public interview reports confirming this problem appears in PayPal loops.

  • Glassdoor (2026)PayPal L4 candidate reported linked-list addition in first technical round
  • Blind (2025)Several PayPal threads mention LC 2 as a canonical phone-screen problem

Problem

You are given two non-empty linked lists representing two non-negative integers stored in reverse order (each node contains a single digit). Add the two numbers and return the sum as a linked list, also in reverse order. You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Constraints

  • The number of nodes in each 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, stored as 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]

Explanation: 9999999 + 9999 = 10009998

Approaches

1. Convert to integers, add, convert back

Traverse each list to build integers, add them, then convert the sum back to a linked list.

Time
O(max(m,n))
Space
O(max(m,n))
// Anti-pattern: JavaScript numbers lose precision beyond 2^53
// Do NOT use this for large inputs
function addTwoNumbers(l1, l2) {
  let s1 = '', s2 = '', curr = l1;
  while (curr) { s1 = curr.val + s1; curr = curr.next; }
  curr = l2;
  while (curr) { s2 = curr.val + s2; curr = curr.next; }
  let sum = (BigInt(s1) + BigInt(s2)).toString();
  let dummy = new ListNode(0), node = dummy;
  for (let i = sum.length - 1; i >= 0; i--) {
    node.next = new ListNode(Number(sum[i]));
    node = node.next;
  }
  return dummy.next;
}

Tradeoff: Requires BigInt for safety; interviewers prefer the in-place carry approach as it scales to arbitrary precision naturally.

2. Simultaneous traversal with carry

Walk both lists digit by digit, maintaining a carry variable. Create a new node for each digit of the sum. After exhausting both lists, create one more node if carry remains. This mirrors how hardware adders work.

Time
O(max(m,n))
Space
O(max(m,n)) for output list
function addTwoNumbers(l1, l2) {
  const dummy = new ListNode(0);
  let curr = dummy;
  let carry = 0;

  while (l1 !== null || l2 !== null || carry !== 0) {
    const val1 = l1 ? l1.val : 0;
    const val2 = l2 ? l2.val : 0;
    const sum = val1 + val2 + carry;

    carry = Math.floor(sum / 10);
    curr.next = new ListNode(sum % 10);
    curr = curr.next;

    if (l1) l1 = l1.next;
    if (l2) l2 = l2.next;
  }

  return dummy.next;
}

Tradeoff: The loop condition `l1 || l2 || carry` elegantly handles lists of unequal length and a final carry bit — emphasize this at PayPal as it reflects correct financial carry-forward logic.

PayPal-specific tips

PayPal interviews focus on payment processing, fraud detection logic, financial reconciliation algorithms, and distributed transaction design. Hash maps, sliding windows, and two-pointer techniques appear frequently.

Common mistakes

  • Stopping the loop when both lists are null but forgetting to emit a final node for the remaining carry
  • Using regular JavaScript numbers instead of BigInt or the carry approach — precision loss for large inputs
  • Not handling lists of different lengths by defaulting missing nodes to 0

Follow-up questions

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

  • What if the numbers are stored in forward order instead of reversed? (LC 445)
  • Multiply two numbers represented as linked lists
  • Add two numbers stored as strings without converting to integers

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 are the digits stored in reverse order?

Reverse order puts the least-significant digit first, which naturally aligns with how carry propagates — you always process the smallest digit first, just like grade-school addition.

How do you handle a carry out of the most significant digit?

Include `carry !== 0` in the while condition. If carry is non-zero after both lists are exhausted, append one more node with carry's value (always 1 for single-digit addition).

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →