class Solution(object):
    def add_one(self, arr):
        """
        :type arr: list[int]
        :rtype: list[int]
        """
        sum = 0
        carry = 1
        arr.reverse()

        for i, c in enumerate(arr):
            sum = c + carry
            arr[i] = sum % 10
            carry = sum // 10
        
        if carry == 1:
            arr.append(1)
        
        arr.reverse()
        return arr

    def add_one2(self, digits):
        """
        :type digits: list[int]
        :rtype: list[int]
        """
        i = len(digits) - 1
        while i >= 0 and digits[i] == 9:
            digits[i] = 0
            i -= 1
        
        if i == -1:
            return [1] + digits

        return digits[:i] + [digits[i] + 1] + digits[i + 1 :]

solution = Solution()
print(solution.add_one2([9, 8, 9]))