Problem overview
The method reverseNumber receives a non-negative integer n up to one billion and returns the number written with its digits in reverse order. 123 becomes 321. A number that ends in zeros loses them: 120 reversed is 021, which is the number 21. One-digit numbers and 0 stay as they are.
Approach: move digits from one number to another
The last digit of n is n % 10, and n / 10 removes it. Taking digits from the right of n yields them in exactly the order the reversed number needs: the last digit of n must become the first digit of result.
To append a digit to the end of result, shift what is already there one place left by multiplying by 10, then add the digit. For example, 32 * 10 + 1 is 321.
Step by step
- Create
resultas anintstarting at 0. Forn = 0the loop never runs, and 0 is the right answer. - Write a
while (n > 0)loop that takes one digit per pass. - Append the last digit:
result = result * 10 + n % 10. - Drop that digit from
nwithn = n / 10. - Return
resultafter the loop.
The solution in Java
class Solution {
public int reverseNumber(int n) {
int result = 0;
while (n > 0) {
result = result * 10 + n % 10;
n = n / 10;
}
return result;
}
}
Tracing n = 123:
| Pass | n at the start | result after |
|---|---|---|
| 1 | 123 | 3 |
| 2 | 12 | 32 |
| 3 | 1 | 321 |
For n = 120 the first digit moved is 0, and 0 * 10 + 0 is still 0. Nothing is stored for that zero, which is why trailing zeros vanish and the method returns 21.
Java details:
- Operator precedence. In
result * 10 + n % 10, both*and%bind tighter than+, so it reads(result * 10) + (n % 10). - Overflow. An
intholds up to 2147483647, and exceeding it wraps silently. Every input within the limits is safe: the reversal of a number of at most 10 digits that is at most one billion stays small. The largest,999999999, reverses to itself. An input outside the limits,1999999999, comes out as1410065399instead of 9999999991, which doesn't fit in anint.
Another way: reverse the text
The digits can also be reversed as text: new StringBuilder("123").reverse() gives 321, and Integer.parseInt turns the text back into a number. Because Integer.parseInt("021") is 21, the trailing zeros of the original number disappear here too. The arithmetic loop avoids the conversion and builds the answer directly.
Tracing a number that ends in zero
For n = 120 the loop runs three times. After the passes, result and n are (0, 12), (2, 1) and (21, 0). The first pass moves the digit 0 into result, and since result was 0 it stays 0. The last pass leaves 21, and that is the answer. Each pass appends exactly one digit, so a number with three digits needs three passes, even when one of those digits is a zero that doesn't show in the final number.
Edge cases and common mistakes
- Trailing zeros.
120gives21, not021. Integers have no leading zeros. - Zero.
reverseNumber(0)returns 0. - Largest input.
1000000000reverses to0000000001, which is1. - Multiplying after adding.
(result + n % 10) * 10adds a stray 0 at the end of the answer. - Forgetting to shrink
n. Withoutn = n / 10the loop never ends.
Testing
123gives321.120gives21.7gives7.0gives0.1000000000gives1.
