Algorithm-Day28-Add-Two-Numbers-lc-2
🧩 Problem Description
You are given two non-empty linked lists representing two non-negative integers.
The digits are stored in reverse order, and each node 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.
💬 Example
1 | Input: l1 = [2,4,3], l2 = [5,6,4] |
💡 Intuition
- Since the numbers are stored in reverse, we can simulate digit-by-digit addition like grade school.
- Maintain a
carry
variable for overflow. - Traverse both lists and construct the result list.
🔢 Java Code
1 | class Solution { |
⏱ Time and Space Complexity
- Time Complexity: O(max(n, m)) — traverses both lists once
- Space Complexity: O(max(n, m)) — result list size
✍️ Summary
- This is a classic linked list and math simulation problem.
- Be comfortable managing carry and dummy nodes.
- Appears frequently in interviews and serves as a great test of logic + list handling.
For follow-ups, consider:
- BigInteger usage (if languages allow)
- Lists in forward order (
lc-445
)