Add Two Numbers - 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.

Input & Output

Example 1 — Basic Addition
$ Input: l1 = [2,4,3], l2 = [5,6,4]
Output: [7,0,8]
💡 Note: 342 + 465 = 807. The digits are stored in reverse order, so [2,4,3] represents 342, [5,6,4] represents 465, and the sum 807 is returned as [7,0,8].
Example 2 — Different Lengths
$ Input: l1 = [0], l2 = [0]
Output: [0]
💡 Note: 0 + 0 = 0. The simplest case where both numbers are zero.
Example 3 — 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. Shows how carries propagate through multiple digits, resulting in a longer output list.

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

Visualization

Tap to expand
Add Two Numbers: Linked List RepresentationInput:l1 = [2,4,3] represents 342243l2 = [5,6,4] represents 465564342 + 465 = 807Process:2 + 5 = 74 + 6 = 10 → 0, carry 13 + 4 + 1 = 8Output:[7,0,8] represents 807708
Understanding the Visualization
1
Input Lists
Two linked lists: [2,4,3] = 342, [5,6,4] = 465
2
Addition Process
Add digit by digit: 2+5=7, 4+6=10 (carry 1), 3+4+1=8
3
Result List
Output [7,0,8] representing 807
Key Takeaway
🎯 Key Insight: Treat linked lists like numbers written backwards and add them digit by digit with carry handling
Asked in
Amazon 85 Microsoft 72 Google 68 Facebook 45
666.0K Views
Very High Frequency
~25 min Avg. Time
18.5K 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