```
public int reverse(int x) {
boolean negative = false;
if ( x < 0 ) negative = true;
x = Math.abs( x );
int max = Integer.MAX_VALUE / 10;
int result = 0;
while( x > 0 ) {
if ( result > max) return 0;
result = result * 10 + ( x % 10 );
x = x / 10;
}
if ( negative ) return -result;
return result;
}
```