Leet Code Reverse Integer Solution in Java

Hello friends, I am going to solve the Leet code Reverse Integer Problem in the Java programming language. It is a very important and medium level question of the Leet Code. This question has been asked in the interview of some popular companies like Adobe, Google, Amazon, Microsoft and Apple.  I'll also explain each step of the code so that you can learn to reverse an integer in the Java Programming Language. So let's start --

Leet Code Reverse Integer Solution in Java

First of all, I recommend you to please read the question carefully and try to solve it on your own. In case, you are not able to solve or you have any problem then read this Leet Code Reverse Integer Solution. If you want to read the question then visit Reverse Integer Leet Code Problem.

In this question, given a signed 32-bit integer (let x). We have to return this integer with its digits reversed. In the next line of the problem, it is mentioned that if reversing x causes the value to go outside the signed 32-bit integer range, then return 0.

Let's take an example: 

Suppose there is an input x = 123, then we have to return the output as 321. If input x = -123, then return -321. Similarly if input = 120 then output will be 21 (021 = 21) and if input x is a single digit (suppose x = 5) then return same number means 5 as it is.

Now I hope you have understood the problem very well. Let's check the solution.

Reverse Integer Solution in Java - Leet Code

public int reverse(int x) {
		//This result will gives us reverse string.
        int ans = 0;

        while (x != 0)
            int digit = x % 10; // Dividing the value by % gives us remainder back so. We can get the last value
            int newAns = ans * 10 + digit; // in the newAns adding the digit value which we got because let say we have something like 
			//this 123, and from digit we got 3. Now what will happen when we add 2 after 3 when we come back we have something like 
			//30 + 2 = 32 and again we come back with carrying 1 we have 320 + 1 = 321, Integer is reversed.
            if ((newAns - digit) / 10 != ans) // checking if it's not valid. Return 0.
                return 0; 
            ans = newAns; // dumping the newAns value in ans
            x = x / 10; // updating our x integer/

        return ans;

First of all, I have declared an integer ans initialized it with 0. In the next line, there is a loop that runs while the input integer x doesn't become 0. 

Inside the while loop, there is an integer digit that stores the last digit of the given input integer x, which we are getting the expression x % 10. 

In the next line, newAns stores the reversed number which we got because let say we have something like 123, and from digit, we got 3. When ans * 10 + digit will run, it will return 3.

In the if condition we are checking if it's not valid then return 0.

In last dumping the newAns value inside ans variable. and updating the input integer in the next line by dividing x by 10. At last return ans.

The time complexity of the above code is BigO(log(N)) and the space complexity is BigO(1).

So this was the solution to the Leetcode Reverse Integer Problem. I hope you have understood. Now try to solve the question again on your own. If you face any problem then please write in the comment.

All the Best on your Coding Journey!

Post a Comment

Previous Post Next Post