def plus_one(digits):
    n = len(digits)
    
    for i in range(n - 1, -1, -1):  # Traverse from last digit to first
        if digits[i] < 9:
            digits[i] += 1
            return digits  # Return early if no carry is needed
        digits[i] = 0  # If digit is 9, set it to 0 and continue

    return [1] + digits  # If all digits were 9, add an extra 1 at the beginning

# Example usage:
print(plus_one([1, 2, 3]))  # Output: [1, 2, 4]
print(plus_one([9, 9, 9]))  # Output: [1, 0, 0, 0]


