def plusOne(digits):
    n = len(digits)
    
    # Start from the last digit and move towards the first
    for i in range(n - 1, -1, -1):
        if digits[i] < 9:
            digits[i] += 1
            return digits
        digits[i] = 0  # If digit is 9, set it to 0 and continue

    # If we exited the loop, it means we had all 9s (e.g., [9, 9, 9] -> [1, 0, 0, 0])
    return [1] + digits

# Example test cases
print(plusOne([1, 2, 3]))  # Output: [1, 2, 4]
print(plusOne([4, 3, 2, 1]))  # Output: [4, 3, 2, 2]
print(plusOne([9]))  # Output: [1, 0]
print(plusOne([9, 9, 9]))  # Output: [1, 0, 0, 0]
