Start learning free

LESSON 217 OF 291 · WHILE LOOPS - CHALLENGES

Reverse a Number in Kotlin: Building It Digit by Digit

Practice Read the full explanation

Given a non-negative integer n, return the number you get when you write its digits in reverse order.

Zeros at the end of n move to the front of the reversed number, where they don't count: 120 reversed is 21. If n is 0, the answer is 0.

 

Example 1:

Input: n = 123
Output: 321
Explanation: The digits 1, 2, 3 read backwards are 3, 2, 1.

Example 2:

Input: n = 120
Output: 21
Explanation: Backwards the digits are 0, 2, 1, and 021 is the number 21.

Example 3:

Input: n = 7
Output: 7
Explanation: A number with one digit stays the same.

 

Constraints:

  • 0 <= n <= 109
STEP 1 OF 6
    Ctrl + Enter to check

    KOTLIN · WHILE LOOPS - CHALLENGES

    Reverse Number in Kotlin, explained

    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:

    Passnum afterresult after
    1123
    2132
    30321

    And for 120:

    Passnum afterresult after
    1120
    212
    3021

    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

    1. Copy n into num with var. Parameters are read-only, and the loop must change this value.
    2. Start result at 0. If n is 0 the loop never runs and 0 is correct.
    3. Loop while num > 0.
    4. Move the last digit over: result = result * 10 + num % 10.
    5. Drop that digit from num with num = num / 10. It is already in result.
    6. 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 *, so result * 10 + num % 10 means (result * 10) + (num % 10).
    • Int overflow wraps silently. A reversed number can be larger than the original, and an Int tops out at 2147483647. The lesson keeps n at or below 1000000000, so the reverse always fits: 999999998 reverses to 899999999. For larger inputs use Long.

    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. 120 gives 21, and 1000000000 gives 1.
    • Shifting the wrong way. result + num % 10 * 10 multiplies the new digit instead of the number built so far.
    • Forgetting to shrink num. The loop runs forever.
    • Single digits and zero. 7 gives 7 and 0 gives 0.

    Testing

    • 123 gives 321.
    • 120 gives 21.
    • 7 gives 7.
    • 0 gives 0.
    • 1000000000 gives 1.
    • 999999998 gives 899999999.
    Keep going with Kotlin1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free