Plus One (LeetCode 66): Solution and Explanation
A non-negative integer is stored as a list of its decimal digits, most significant first, with no leading zeros. Add one to the number and return the resulting list of digits.
Watch it run step by step
Try your own input and write the code →Intuition
You could convert the list to an integer, add one and convert back. In Python that even works for large values, but in Java or C++ a hundred-digit number overflows every built-in integer type, and the problem is clearly asking you to simulate addition directly on the digits.
Think about how you add one by hand. You start at the last digit. If it is less than 9, you bump it and you are done, because no carry is produced and the rest of the number does not change. If it is a 9, it becomes 0 and a carry of 1 moves to the next digit to the left, where the same decision repeats.
That means the work stops at the first digit from the right that is not a 9, which makes the common case very fast. The only case where the carry escapes the whole list is when every digit is 9, like 999. Then every position turns to 0 and the answer needs one extra digit in front: 1000. That is the only time a new list is created.
Because the function returns as soon as a digit absorbs the carry, the loop never needs a separate carry variable. Reaching the end of the loop is itself the signal that a carry is still pending, which is why the final line can unconditionally prepend a 1.
Approach: Right-to-left carry
- Loop
ifrom the last index down to 0. - If
digits[i]is less than 9, increment it and returndigitsright away, since there is no further carry. - Otherwise the digit is 9, so set it to 0 and let the loop continue to carry into the next digit to the left.
- If the loop finishes, every digit was 9 and is now 0, so return
[1] + digits, adding a leading 1.
Step-by-step example
digits = [1,9,9]
- At index 2 the digit is 9, so it becomes 0 and the carry moves left. The list is [1,9,0].
- At index 1 the digit is also 9, so it becomes 0. The list is [1,0,0].
- At index 0 the digit is 1, which is less than 9, so it becomes 2 and the function returns [2,0,0].
- For comparison, [9,9] would turn both digits to 0, finish the loop, and return [1,0,0].
Code: Python, Java, C++
1class Solution:
2 def plusOne(self, digits: List[int]) -> List[int]:
3 for i in range(len(digits) - 1, -1, -1):
4 if digits[i] < 9:
5 digits[i] += 1
6 return digits
7 digits[i] = 0
8
9 return [1] + digits1class Solution {
2 public int[] plusOne(int[] digits) {
3 for (int i = digits.length - 1; i >= 0; i--) {
4 if (digits[i] < 9) {
5 digits[i]++;
6 return digits;
7 }
8 digits[i] = 0;
9 }
10
11 int[] result = new int[digits.length + 1];
12 result[0] = 1;
13 return result;
14 }
15}1class Solution {
2public:
3 vector<int> plusOne(vector<int>& digits) {
4 for (int i = digits.size() - 1; i >= 0; i--) {
5 if (digits[i] < 9) {
6 digits[i]++;
7 return digits;
8 }
9 digits[i] = 0;
10 }
11
12 digits.insert(digits.begin(), 1);
13 return digits;
14 }
15};Time and space complexity
O(n)
In the worst case, when every digit is 9, the loop visits all n digits and then builds a list of length n + 1. Usually it stops much earlier, at the first non-nine from the right.
O(1)
The digits are updated in place. Only in the all-nines case is a new list of n + 1 digits created, which is the output itself.
Other approaches
Explicit carry variable (time O(n), space O(1))
Keep a carry that starts at 1, and for each digit from the right compute total = digits[i] + carry, store total % 10 and set carry = total // 10. It is longer but generalizes directly to adding any number to the digit list.
Convert to an integer (time O(n), space O(n))
Join the digits, convert to an integer, add one and split back into digits. It is short in Python but overflows fixed-width integers in other languages and misses the point of the exercise.
Common mistakes
- Forgetting the all-nines case and returning [0,0,0] for [9,9,9]. The extra leading 1 is required.
- Continuing the loop after incrementing a digit below 9, which can add the carry more than once.
- Iterating from the front of the list. Addition carries from the least significant digit, which is at the end.
- Building a new reversed list and forgetting to reverse it back, which returns the digits in the wrong order.
Frequently asked questions
How do you handle 999 in Plus One?
Every 9 turns to 0 as the carry moves left, and when the loop finishes there is still a carry. The function returns a new list with a 1 in front, giving [1,0,0,0].
Why not convert the digits to an integer?
The list can hold up to 100 digits, which overflows 64-bit integers in Java and C++. Working digit by digit avoids overflow and runs in linear time in any language.
What is the time complexity of Plus One?
It is O(n) in the worst case, when all digits are 9, and O(1) on average because the loop usually stops at the last digit. Extra space is O(1) except for the all-nines case, which returns a new list.
Can the result ever be longer than one extra digit?
No. Adding one to an n-digit number produces at most n + 1 digits, and that only happens when the number is all nines.