✅ Beats 97.34% || Only Python || Math✅|| Easy to understand 🔥🔥🔥🔥
\uD83D\uDD3C IF YOU FIND THIS POST HELPFUL PLEASE UPVOTE \uD83D\uDC4D\n\n\n\n\n# Complexity\n### 1.Time complexity:\n Add your time complexity here, e.g. O(n) \
# \uD83D\uDD3C IF YOU FIND THIS POST HELPFUL PLEASE UPVOTE \uD83D\uDC4D\n![image.png](https://assets.leetcode.com/users/images/56bbc7fb-c62e-474a-b987-2566cb890d60_1712393323.6683912.png)\n\n\n\n# Complexity\n### 1.Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n- **Best Case (O(1))**: If the last digit is not `9` (i.e., any number less than `9`), the code increments it directly in constant time (`digits[i] += 1`) and immediately returns the modified list. This scenario happens when the input list represents a number less than `9` followed by zeros (e.g., [1, 0, 0]).\n- **Worst Case (O(n))**: If all digits are `9s` (i.e., the input represents a number like `999`), the code iterates through the entire list (len(digits)) in the for loop, setting each digit to `0`. Since the loop doesn\'t break after the first increment, it continues until the end. This results in a linear time complexity.\n\n### 2. Space Complexity:\n\n- **In-Place Modification**: The code modifies the original digits list in-place, potentially avoiding the need for additional space allocation. In the best case where only one digit is incremented, the space complexity remains constant (`O(1)`).\n- **Worst Case (O(1))**: Even in the worst case (all `9s`), the code doesn\'t create a new list. It modifies the existing list in-place. Therefore, the space complexity remains `O(1)`.\n\n\n\n\n# Code\n```\nclass Solution:\n    def plusOne(self, digits: List[int]) -> List[int]:\n        for i in reversed(range(len(digits))):\n            if digits[i] == 9:\n                digits[i] = 0\n            else:\n                digits[i] += 1\n                return digits\n        return [1] + digits\n```\n\n### \uD83D\uDD3C If you found my solution and information helpful, I would greatly appreciate your upvote, as it would motivate me to continue sharing more solutions. \uD83D\uDD3C\n![upvote.jpg](https://assets.leetcode.com/users/images/a13ab0b5-6f2b-4804-87d2-999b78e64297_1712149472.8021562.jpeg)
['Array', 'Math', 'Python3']