Leetcode Reverse Integer problem solution in Java programming

In the Leetcode Reverse Integer problem solution in Java programming Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 – 1], then return 0.

Leetcode Reverse Integer problem solution in Java programming

class Solution {
    public int reverse(int x) {
        long sign=1;
        long result=0;
        if(x<0) {
            sign=-1;
            x*=-1;
        }
        while(x>0){
            result=result*10+x%10;
            x/=10;
            if(result>Integer.MAX_VALUE) return 0;
        }

        return (int)(sign*result);
    }
}

Also read,

By Neha Singhal

Hi, my name is Neha singhal a software engineer and coder by profession. I like to solve coding problems that give me the power to write posts for this site.

Leave a Reply

Your email address will not be published. Required fields are marked *