Solving tips
- Walk right-to-left: the first digit less than 9 can just be incremented and returned, since nothing else changes.
- Zero out each trailing 9 as you pass it; the carry only ripples left through consecutive 9s.
- The only case the array grows is all-nines: after the loop, prepend a leading 1 (e.g. [9,9] -> [1,0,0]).
- Operate on the digit list directly to avoid overflow; O(n) time worst case, O(1) extra space.
Problem
You are given a non-negative integer represented as a list of its decimal digits digits, most-significant digit first. The number has no leading zeros (except the number 0 itself, given as [0]). Add one to the number and return the resulting list of digits.
Examples
digits = [1, 2, 3] β [1, 2, 4] β 123 + 1 = 124.
digits = [4, 3, 9] β [4, 4, 0] β 439 + 1 = 440; the trailing 9 rolls over and carries.
digits = [9, 9] β [1, 0, 0] β 99 + 1 = 100; the carry propagates off the front, growing the array.
Constraints
1 <= len(digits) <= 100
0 <= digits[i] <= 9
digits has no leading zeros (aside from [0]).
Think about it first
Hint 1
Adding one only affects the end of the number, and it only keeps rippling left while it hits digits equal to 9 (which become 0 and pass a carry along).
Hint 2
Walk from the last digit toward the first. The first digit less than 9 can simply be incremented, and you're done β every digit to its right stays as it is.
Hint 3
The only case where the array grows is all-nines: they all become 0 and you must prepend a leading 1, turning `[9,9,9]` into `[1,0,0,0]`.
TL;DR
Walk right-to-left, incrementing the first non-9 and zeroing the trailing 9s β O(n) time, O(1) extra space.
Approach 1 β Brute force (convert to an integer)
The obvious route: join the digits into an integer, add one, and split the result back into digits.
from typing import List
class Solution:
def plusOne(self, digits: List[int]) -> List[int]:
num = int("".join(map(str, digits))) + 1
return [int(c) for c in str(num)]
Complexity: O(n) time, O(n) space. It works, but it detours through string/int conversions and, in fixed-width-integer languages, would overflow for large inputs β the whole point of representing the number as digits is to avoid that.
Approach 2 β In-place carry from the right
The insight: adding one propagates a carry leftward only through consecutive trailing 9s. The first digit from the right that is less than 9 absorbs the carry β increment it and return immediately, since nothing to its right or left changes. If every digit is 9, they all become 0 and a single leading 1 is prepended.
from typing import List
class Solution:
def plusOne(self, digits: List[int]) -> List[int]:
for i in range(len(digits) - 1, -1, -1):
if digits[i] < 9:
digits[i] += 1
return digits
digits[i] = 0
return [1] + digits
Walkthrough with digits = [4, 3, 9]:
i = 2: digits[2] = 9, not < 9, so set it to 0 β [4, 3, 0].
i = 1: digits[1] = 3 < 9, so increment β [4, 4, 0] and return.
With digits = [9, 9]:
i = 1: 9 β 0 β [9, 0].
i = 0: 9 β 0 β [0, 0].
- Loop ends without returning, so prepend
1 β [1, 0, 0].
Complexity: O(n) time worst case (all nines), often O(1) when the last digit isnβt 9; O(1) extra space apart from the one new cell prepended in the all-nines case.
Common pitfalls
- Handling the all-nines case by mutating in place and forgetting to prepend the leading
1.
- Iterating left-to-right and trying to track a carry forward β right-to-left is far simpler because the carry naturally flows toward the most-significant end.
- Returning after zeroing a 9 instead of continuing the loop; you only return once youβve found a digit to increment.
Pattern takeaway
Grade-school addition on a digit array runs right-to-left with a carry. Recognize that β+1β is the cheapest form of this: it stops at the first non-9 digit, and only the all-nines input changes the arrayβs length. Working on the digit list directly avoids any integer-overflow concerns.