#2

Add Two Numbers

Medium Digit-by-digit with carry

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Two non-negative numbers are stored as linked lists with digits in reverse order (ones digit first). Return their sum as a list in the same format.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: add two numbers stored backwards as lists of digits.

Why this works: the ones digit comes first, so add digit by digit with a carry, just like on paper.

Example: 2→4→3 (342) + 5→6→4 (465)

2+5 = 7, 4+6 = 10 → 0 carry 1, 3+4+1 = 87 → 0 → 8 (807)

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Stopping when both lists end and forgetting a final carry (e.g. 5 + 5 = 10 needs a new node).

Solve on LeetCode ↗
Complexity
  • Time: O(max(n, m)).
  • Space: O(max(n, m)) for the output.
Topic: Linked List
🎬 Episodes 40–45: video coming soon