public class Solution {

public int reverse(int x) {

int sign = x < 0? -1:1;

x = x < 0? -x : x;

long result = 0;

while (x > 0){

long digit = x % 10;

result = result * 10 + digit;

x /= 10;

}

if (result > (long) Integer.MAX_VALUE){

return 0;

}

return sign * (int) result;

}

}