Problem overview
The function reverseNumber receives a non-negative integer n and returns the number you get by writing its digits backwards. 123 becomes 321. Zeros at the end of n land at the front of the reversed number, where they don't count, so 120 becomes 21. The number 0 stays 0.
Approach
Take the digits off n from the right and push each one onto the end of a new number. The last digit of n is the first digit of the answer, so peeling digits from the right already gives the right order.
Two operations do it. num % 10 is the last digit of num, and num / 10 removes it. To append a digit to result, shift it left by multiplying by 10 and add the digit: result * 10 + digit.
The values after each pass for 123:
| Pass | num after | result after |
|---|---|---|
| 1 | 12 | 3 |
| 2 | 1 | 32 |
| 3 | 0 | 321 |
And for 120:
| Pass | num after | result after |
|---|---|---|
| 1 | 12 | 0 |
| 2 | 1 | 2 |
| 3 | 0 | 21 |
The first digit moved is the 0, and 0 * 10 + 0 is still 0, so the trailing zero vanishes with no special code.
Step by step
- Copy
nintonumwithvar. Parameters are read-only, and the loop must change this value. - Start
resultat 0. Ifnis 0 the loop never runs and 0 is correct. - Loop while
num > 0. - Move the last digit over:
result = result * 10 + num % 10. - Drop that digit from
numwithnum = num / 10. It is already inresult. - Return
result.
The solution in Kotlin
class Solution {
fun reverseNumber(n: Int): Int {
var num = n
var result = 0
while (num > 0) {
result = result * 10 + num % 10
num = num / 10
}
return result
}
}
Some details worth knowing:
- Operator precedence.
%binds tighter than+, like*, soresult * 10 + num % 10means(result * 10) + (num % 10). Intoverflow wraps silently. A reversed number can be larger than the original, and anInttops out at 2147483647. The lesson keepsnat or below 1000000000, so the reverse always fits:999999998reverses to899999999. For larger inputs useLong.
Why the multiply by 10 works
Think of result as the reversed number built so far. Multiplying a decimal number by 10 slides every digit one place left and opens a 0 in the last place. Adding a digit between 0 and 9 then fills that place without disturbing the others. So 32 * 10 + 1 is 321: the 3 and the 2 move over, and the 1 lands at the end.
Each pass therefore takes the digit that was last in num and makes it the last digit of result. The digit taken first ends up leftmost, and the digit taken last ends up rightmost. That is exactly a reversal.
This also explains the trailing zeros. While result is still 0, shifting it left and adding a 0 changes nothing, so the leading zeros of the reversed number never show up. The number 021 is the number 21.
Complexity
Time is O(d) for d digits, one pass each. Space is O(1).
Edge cases and common mistakes
- Trailing zeros.
120gives21, and1000000000gives1. - Shifting the wrong way.
result + num % 10 * 10multiplies the new digit instead of the number built so far. - Forgetting to shrink
num. The loop runs forever. - Single digits and zero.
7gives 7 and0gives 0.
Testing
123gives321.120gives21.7gives7.0gives0.1000000000gives1.999999998gives899999999.
