This is like a basic maths, I traversed the list and added the correct place value of subsequent digits to the previous ones, making the initial digit the 1 that should be added to the whole digits.
I got the place value by raising 10 to the power of (the length of the list − 1 − the index).
Then, I built a list from the resulting total digits and returned it.
class Solution:
def plusOne(self, digits: List[int]) -> List[int]:
start = 1
n = len(digits)
for i in range(n):
start += digits[i] * (10 ** (n -i - 1))
return [int(d) for d in str(start)]
- Time: O(n) - traversing the list of digits
- Space: O(n) - Building a list of the resulting digit