PYTHON || 100 BEAT %🔥🔥|| 100% EFFICIENT🔥|| OPTIMAL SOLUTION || EASY TO UNDERSTAND ||
if it\'s help, please up \u2B06vote!\u2764\uFE0F and comment\uD83D\uDD25\n# Intuition\n Describe your first thoughts on how to solve this problem. \n- When thin
# if it\'s help, please up \u2B06vote!\u2764\uFE0F and comment\uD83D\uDD25\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- When thinking about incrementing a number represented as an array of digits, the simplest approach is to traverse the array from the end to the start. This way, you handle the least significant digit first, which is where a carry (if any) would occur.\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- Start from the last digit of the array and move towards the first digit.\n- If the current digit is 9, set it to 0 because 9 + 1 results in a carry over.\n- If the current digit is not 9, increment it by 1 and return the array immediately, as there will be no carry over.\n- If all digits are processed and all were 9, prepend 1 to the array to handle the final carry over.\n# Complexity\n- Time complexity: O(n), where n is the number of digits in the array. This is because in the worst case, you might need to traverse all the digits once (when all the digits are 9).\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1) in terms of additional space, since the operation is performed in place except in the worst case scenario where an extra digit is added (e.g., from 999 to 1000).\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution(object):\n    def plusOne(self, digits):\n        """\n        :type digits: List[int]\n        :rtype: List[int]\n        """\n        # Traverse the list from the last element to the first\n        for i in range(len(digits)-1, -1, -1):\n            # If the current digit is 9, set it to 0\n            if digits[i] == 9:\n                digits[i] = 0\n            else:\n                # If the current digit is not 9, increment it by 1 and return the list\n                digits[i] = digits[i] + 1\n                return digits\n        # If all digits are 9, prepend 1 to the list\n        return [1] + digits\n\n```\n![7c17f983-3b8b-4738-b722-c1c1466f9510_1682940288.2823734.jpeg](https://assets.leetcode.com/users/images/55b80601-7c3d-427d-8efe-85c1dca9f5a4_1719210527.276095.jpeg)\n
['Array', 'Math', 'Python', 'Python3']