```
class Solution {
public:
int reverse(int x) {
long long int result = 0;
while (x) {
result = 10 * result + x % 10;
x = x / 10;
}
return result > std::numeric_limits<int>::max() || result < std::numeric_limits<int>::min() ? 0 : result;
}
};
```