```
public class Solution {
public int reverse(int x) {
int res = 0;
while (x != 0) {
if (res*10/10 != res) return 0; // if res*10 overflows, return 0
res = 10*res + x%10;
x /= 10;
}
return res;
}
}
```