|
| 1 | +# Add Two Numbers |
| 2 | + |
| 3 | +[](https://leetcode.com/problems/add-two-numbers/) |
| 4 | +[](https://leetcode.com/problemset/?difficulty=MEDIUM) |
| 5 | +[](https://leetcode.com/problems/add-two-numbers/) |
| 6 | + |
| 7 | +**Problem Number:** [2](https://leetcode.com/problems/add-two-numbers/) |
| 8 | +**Difficulty:** [Medium](https://leetcode.com/problemset/?difficulty=MEDIUM) |
| 9 | +**Category:** Linked List, Math, Recursion |
| 10 | +**LeetCode Link:** [https://leetcode.com/problems/add-two-numbers/](https://leetcode.com/problems/add-two-numbers/) |
| 11 | + |
| 12 | +## Problem Description |
| 13 | + |
| 14 | +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. |
| 15 | + |
| 16 | +You may assume the two numbers do not contain any leading zero, except the number 0 itself. |
| 17 | + |
| 18 | +**Example 1:** |
| 19 | +``` |
| 20 | +Input: l1 = [2,4,3], l2 = [5,6,4] |
| 21 | +Output: [7,0,8] |
| 22 | +Explanation: 342 + 465 = 807 |
| 23 | +``` |
| 24 | + |
| 25 | +**Example 2:** |
| 26 | +``` |
| 27 | +Input: l1 = [0], l2 = [0] |
| 28 | +Output: [0] |
| 29 | +``` |
| 30 | + |
| 31 | +**Example 3:** |
| 32 | +``` |
| 33 | +Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9] |
| 34 | +Output: [8,9,9,9,0,0,0,1] |
| 35 | +``` |
| 36 | + |
| 37 | +**Constraints:** |
| 38 | +- The number of nodes in each linked list is in the range [1, 100] |
| 39 | +- 0 <= Node.val <= 9 |
| 40 | +- It is guaranteed that the list represents a number that does not have leading zeros |
| 41 | + |
| 42 | +## My Approach |
| 43 | + |
| 44 | +I used an **iterative approach** to simulate the manual addition process we learned in elementary school. The key insight is to process both linked lists digit by digit, maintaining a carry value, and building the result linked list as we go. |
| 45 | + |
| 46 | +**Algorithm:** |
| 47 | +1. Initialize a carry variable to 0 |
| 48 | +2. Create a dummy head node for the result linked list |
| 49 | +3. Iterate through both linked lists simultaneously: |
| 50 | + - Extract the current digit from each list (0 if list is exhausted) |
| 51 | + - Add the digits along with the carry |
| 52 | + - Calculate the new carry and the digit to store |
| 53 | + - Create a new node with the calculated digit |
| 54 | + - Append it to the result list |
| 55 | +4. If there's still a carry after processing all digits, create a final node |
| 56 | +5. Return the head of the result linked list |
| 57 | + |
| 58 | +## Solution |
| 59 | + |
| 60 | +The solution uses an iterative approach to add two numbers represented as linked lists. See the implementation in the [solution file](../exercises/2.add-two-numbers.py). |
| 61 | + |
| 62 | +**Key Points:** |
| 63 | +- Processes both lists simultaneously, handling different lengths |
| 64 | +- Maintains a carry value throughout the addition process |
| 65 | +- Builds the result linked list incrementally |
| 66 | +- Handles the final carry if it exists |
| 67 | +- Returns the result in reverse order (least significant digit first) |
| 68 | + |
| 69 | +## Time & Space Complexity |
| 70 | + |
| 71 | +**Time Complexity:** O(max(M, N)) |
| 72 | +- We iterate through both linked lists once |
| 73 | +- M and N are the lengths of the input linked lists |
| 74 | +- Each iteration performs constant time operations |
| 75 | + |
| 76 | +**Space Complexity:** O(max(M, N)) |
| 77 | +- We create a new linked list to store the result |
| 78 | +- The result can be at most max(M, N) + 1 digits long (due to carry) |
| 79 | +- We use a constant amount of extra space for variables |
| 80 | + |
| 81 | +## Key Insights |
| 82 | + |
| 83 | +1. **Reverse Order Advantage:** The fact that digits are stored in reverse order makes the addition process straightforward - we can process from left to right, just like manual addition. |
| 84 | + |
| 85 | +2. **Carry Management:** The carry must be tracked and added to the next position, similar to how we learned addition in school. |
| 86 | + |
| 87 | +3. **Different Lengths:** The solution handles cases where the input lists have different lengths by treating missing digits as 0. |
| 88 | + |
| 89 | +4. **Final Carry:** After processing all digits, if there's still a carry, it becomes the most significant digit in the result. |
| 90 | + |
| 91 | +5. **Linked List Construction:** Building the result linked list incrementally is more efficient than converting to integers and back. |
| 92 | + |
| 93 | +6. **No Leading Zeros:** The problem guarantees no leading zeros in input, but we need to handle the case where the result might have a leading zero (like 0 + 0 = 0). |
| 94 | + |
| 95 | +## Mistakes Made |
| 96 | + |
| 97 | +1. **Carry Calculation:** The initial approach of converting the sum to a string to extract carry and digit is inefficient. A more efficient approach would be to use integer division and modulo operations. |
| 98 | + |
| 99 | +2. **Variable Naming:** The variable names could be more descriptive to improve code readability. |
| 100 | + |
| 101 | +3. **Edge Case Handling:** Need to ensure proper handling when one list is longer than the other. |
| 102 | + |
| 103 | +## Related Problems |
| 104 | + |
| 105 | +- **Add Two Numbers II** (Problem 445): Similar problem but digits are stored in forward order |
| 106 | +- **Multiply Strings** (Problem 43): Multiplying two numbers represented as strings |
| 107 | +- **Plus One** (Problem 66): Adding one to a number represented as an array |
| 108 | +- **Add Binary** (Problem 67): Adding two binary strings |
| 109 | +- **Add Strings** (Problem 415): Adding two numbers represented as strings |
| 110 | + |
| 111 | +## Alternative Approaches |
| 112 | + |
| 113 | +1. **Recursive Solution:** Can be solved recursively by processing one digit at a time |
| 114 | +2. **Convert to Integer:** Convert linked lists to integers, add them, then convert back (not recommended due to potential overflow) |
| 115 | +3. **Stack-based:** Use stacks to reverse the order and then add (useful for forward order problems) |
| 116 | + |
| 117 | +## Common Pitfalls |
| 118 | + |
| 119 | +1. **Forgetting Final Carry:** Always check if there's a carry after processing all digits |
| 120 | +2. **Null Pointer Exceptions:** Ensure proper null checks when accessing list nodes |
| 121 | +3. **Memory Management:** Be careful not to lose references when building the result list |
| 122 | +4. **Overflow:** While the problem constraints prevent integer overflow, it's good practice to consider it |
| 123 | + |
| 124 | +--- |
| 125 | + |
| 126 | +[](../../README.md#-problem-index) | [](../exercises/2.add-two-numbers.py) |
| 127 | + |
| 128 | +*Note: This is a fundamental linked list problem that teaches the importance of careful iteration and carry management in mathematical operations.* |
0 commit comments