Simple solution in Java


  • 0
    N
    class Solution {
        public int reverse(int x) {
            
            if(x==0)
                return 0;
            
            long rev=0;
            while(x!=0)
            {
                rev = rev*10 + x%10;
                
                if(rev>=Integer.MAX_VALUE || rev<=Integer.MIN_VALUE)
                    return 0;
                
                x=x/10;
            }
            return (int)rev;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.