//Make use of Integer.MAX_VALUE

long p = x;

boolean isNegtive = false;

if(p < 0){

p = 0 - p;

isNegtive = true;

}

```
long answer = 0;
while(p != 0){
answer *= 10;
answer += p % 10;
p /= 10;
}
if(answer > Integer.MAX_VALUE) return 0;
return isNegtive ? 0 - ((int)answer) : (int)answer;
```