```
class Solution {
public:
int reverse(int x) {
long long sign = x >= 0 ? 1 : -1;
long long ret = 0, val = x * sign; \\ make x as a positive, could overflow if x = x * sign when x == INT_MIN
while (val)
{
ret = ret * 10 + val % 10;
val /= 10;
}
ret *= sign;
return (int)((ret < INT_MIN || ret > INT_MAX) ? 0 : ret);
}
};
```