Reverse Integer | LeetCode 7

In this question, we need to reverse an integer.

The key to solving this problem is by creating a variable, let’s call it reversed, which will slowly be transformed until it is the reverse of the integer in question. Let’s walk through this using the number 321.

No matter which integer you want to reverse, the reversed variable will always be initialized to 0. Then, in this example, it will be 1, then 12, then 123.

Please note that the method described above will only work on positive integers. In order to make it work with a negative integer, we can first make the integer positive and then after we find the reverse of it, multiple its reverse by -1 in order to make it negative. For example, if we wanted to reverse -456, we’d first make it 456, reverse it to 654, and then multiply that by -1 to make the final result -654.

Now, to the code. We can start with the function skeleton provided by LeetCode.

/**
 * @param {number} x
 * @return {number}
 */
var reverse = function(x) {};

This function expects one argument, x, which is an integer, and it returns the reversed form of that integer.

As mentioned above, we need to know whether x is negative. We can do this by checking whether x is less than 0, and saving that in a variable we’ll call negative.

let negative = x < 0;

Now, let’s create the reversed variable we need to transform into the reversed number. This is always initialized to 0.

let reversed = 0;

At this point, let’s make x a positive integer if it’s negative.

if (negative) {
    x *= -1;
}

Now comes the tricky part. How do we actually reverse an integer? I’ll illustrate that below.

Let’s say we want to reverse the number 321, like we mentioned above. We need to keep track of two things: the reversed number and the original number, x.

The reversed form of 321 is 123, so the first number we need is 1. We do this dividing 321 by 10 and saving the remainder. What’s the remainder of 321 divided by 10? 1.

At the same time, we need to add this to 10 times whatever reversed currently is. Right now, reversed is 0, so the math would look like this: (321 % 10) + (0 * 10).

1 + 0 is 1! This is exactly what we need.

reversed = 1

Here it is in code:

reversed = (reversed * 10) + (x % 10);

All we have left to do in this iteration is chop off the last integer of the original number (x) so that we can repeat the steps above. We do that by dividing it by 10 and rounding down.

x = 32

Here that is in code:

x = Math.floor(x / 10);

The first iteration is done. Our variable reversed is 1 and the original number, x, is now 32.

We just repeat these steps over and over until we’ve completely reversed our integer. We can go a little more quickly now that we’ve done it once.

Our reversed integer is 1, but now it needs to be 12. We get the number 2 we’re looking for by getting the remainder of 32 divided by 10. 32 % 10 = 2.

But remember that reversed is still 1 at this point, so we can’t just add 2 to it. That would be 3. We need to first multiply reversed by 10, which makes it 10, and then add the 2. This gives us the 12 we need. (32 % 10) + (1 * 10) = 12.

reversed = 12

And before we move to the next iteration, we chop off the last digit of our original number, x, which at this point is still 32. Remember we do this by dividing it by 10 and rounding down.

x = 3.

Since we’re pros at this now, let’s go through the explanation of the last iteration very quickly. Our reversed number is 12, which is very close to the 123 we need. We multiply reversed by 10, getting us to 120. Then we look at what’s left of our original number, x, which is now just 3. We grab this 3 by dividing reversed by 10 and getting the remainder. Then we add that 3 to 120, giving us the 123 we need. Then we finish off by dividing our original integer, x (which is now just 1) by 10 and rounding down, bringing it to 0. That’s exactly, by the way, the condition we add in the code to know when to stop — when x is no longer greater than 0.

Here’s that entire block of code.

while (x > 0) {
    reversed = (reversed * 10) + (x % 10);
    x = Math.floor(x / 10);
}

We’re close to being done entirely done, but we need to make sure our newly reversed integer is within the 32-bit signed integer range of 2^31 – 1, since that’s what the question states. All we have to do is check that with an if statement and return 0 if it’s outside of that range.

if (reversed > (2 ** 31 - 1)) {
    return 0;
}

Now we’re at the end. We’ve successfully reversed our integer, but remember that if it was originally negative, we made it positive. We just need to check our negative variable which held whether or not it was originally negative. If it was originally negative, we need to make our result negative. If it wasn’t originally negative, we can just return our already-positive reversed result.

 return negative ? (reversed * -1) : reversed;

And that’s it! The full implementation is at the bottom of this page, but if you want to support us and are interested in the resources we use to make this content, check out the links below.

AFFILIATE LINKS

Great resource I use to learn algorithms.
40% off Tech Interview Pro: http://techinterviewpro.com/terriblewhiteboard
20% off CoderPro: http://coderpro.com/terriblewhiteboard


Last, but not least, here is the full implementation.

/**
 * @param {number} x
 * @return {number}
 */
var reverse = function(x) {    
    let negative = x < 0;
    let reversed = 0;
    
    if (negative) {
        x *= -1;
    }
    
    while (x > 0) {
        reversed = (reversed * 10) + (x % 10);
        x = Math.floor(x / 10);
    }
    
    if (reversed > (2 ** 31 - 1)) {
        return 0;
    }
    
    return negative ? (reversed * -1) : reversed;
};

Link to problem on LeetCode: https://leetcode.com/problems/reverse-integer/