```
public int reverse(int x) {
long result = 0;
int sign = x > 0? 1: -1;
x *= sign;
int mod = 0;
while (x > 0) {
mod = x % 10;
result = result*10 + mod;
x /= 10;
}
if (result > 2147483647) {
return 0;
}
return sign * (int)result;
}
```