Imagine you have two large numbers written on paper, but instead of storing them as regular integers, each digit is stored in a separate box (node) of a linked list. The twist? The most significant digit comes first - just like how we normally write numbers!
You're given two non-empty linked lists representing two non-negative integers. Each node contains a single digit (0-9), and you need to add these two numbers together and return the sum as a new linked list.
For example:
- Number 1:
7 → 2 → 4 → 3represents 7243 - Number 2:
5 → 6 → 4represents 564 - Sum:
7 → 8 → 0 → 7represents 7807
The challenge is that we can't simply reverse the lists (like in Add Two Numbers I) because we need to maintain the original structure. We need to handle carries that propagate from right to left, just like elementary school addition!
Input & Output
Visualization
Time & Space Complexity
Need to traverse both lists once to fill stacks, then process all digits once more
Space for two stacks to hold all digits from both lists
Constraints
- The number of nodes in each linked list is in the range [1, 100]
- 0 ≤ Node.val ≤ 9
- The input represents a valid number without leading zeros
- Follow up: Could you solve it without reversing the input lists?