Add Two Numbers - Problem

Imagine you're building a calculator that works with really big numbers - so big that regular integers can't handle them! In this problem, you're given two numbers represented as linked lists, where each node contains a single digit, and the digits are stored in reverse order.

For example, the number 342 would be represented as 2 → 4 → 3 in the linked list.

Your task is to add these two numbers and return the result as a new linked list, also in reverse order. Don't forget to handle carry-over when the sum of two digits exceeds 9!

Goal: Add two numbers represented as reversed linked lists
Input: Two non-empty linked lists representing non-negative integers
Output: A new linked list representing the sum in reverse order

Input & Output

example_1.py — Basic Addition
$ Input: l1 = [2,4,3], l2 = [5,6,4]
Output: [7,0,8]
💡 Note: 342 + 465 = 807. The linked lists represent numbers in reverse order, so we're actually adding 342 + 465 = 807, which becomes [7,0,8] in reverse order.
example_2.py — Different Lengths
$ Input: l1 = [0], l2 = [0]
Output: [0]
💡 Note: Simple case: 0 + 0 = 0
example_3.py — Carry Propagation
$ Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
Output: [8,9,9,9,0,0,0,1]
💡 Note: 9999999 + 9999 = 10009998. Multiple carries propagate through the addition, resulting in a longer result list.

Visualization

Tap to expand
Adding 342 + 465 = 807Traditional Paper Addition: 3 4 2+ 4 6 5 8 0 7↑ carryLinked List Representation:342 → [2] → [4] → [3]465 → [5] → [6] → [4]807 → [7] → [0] → [8]Step-by-Step ProcessPosition 0: 2 + 5 = 7 (carry = 0)Position 1: 4 + 6 = 10 (digit = 0, carry = 1)Position 2: 3 + 4 + 1(carry) = 8Result: [7] → [0] → [8]💡 Key Advantage: Digits are already in the perfect order for addition!
Understanding the Visualization
1
Start from the least significant digits
Both linked lists start with the ones place, making addition straightforward
2
Add digits and handle carry
Add current digits plus any carry from previous position
3
Create result node
Store the digit (sum % 10) and calculate new carry (sum / 10)
4
Continue until done
Process all digits and any final carry
Key Takeaway
🎯 Key Insight: The reverse order storage in linked lists perfectly matches how we perform manual addition - from right to left with carry propagation!

Time & Space Complexity

Time Complexity
⏱️
O(max(m,n))

We traverse both lists once, where m and n are the lengths of the two lists

n
2n
Linear Growth
Space Complexity
O(max(m,n))

Space for the result linked list, which has at most max(m,n)+1 nodes

n
2n
Linearithmic Space

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
  • Both input lists are non-empty
Asked in
Google 127 Amazon 89 Microsoft 76 Meta 54
425.9K Views
Very High Frequency
~15 min Avg. Time
18.9K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen