Java solution. Check for overflow at the end


  • 0
    D
    public int reverse(int x) {
        int rev = 0; 
        int prev = 0;
        while(x != 0){
            prev = rev; 
            rev *= 10; 
            rev += x%10; 
            x /= 10;
        }
        if(prev*10/10 !=  prev)  return 0;
        return rev; 
    }

Log in to reply
 

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