This solution would work if you have been allowed to use 64 bits instead of 32 bit

```
public class Solution {
static int max = Integer.MAX_VALUE;
static int min = Integer.MIN_VALUE;
public int reverse(int x) {
int r = 0;
if(x>1000000000 && x%10>2) return 0; // optimization only for 32 bit
if(x< -1000000000 && x%10<-2) return 0; // optimization only for 32 bit
while(x != 0)
{
if((r*10)/10 != r) return 0; // check overflow without long
r = r*10 + x%10;
x = x/10;
}
return r;
}}
```