```
class Solution {
public:
int reverse(int x) {
if(x/10 == 0) return x;
long sum = 0;
while(x != 0){
int tmp = x % 10;
sum = sum * 10 + tmp;
x /= 10;
}
if(sum > INT_MAX || sum < INT_MIN) return 0;
return (int)sum;
}
};
```