we can slove this problem like Hexadecimal conversion

MY CODE

```
class Solution {
public:
int reverse(int x) {
long long ans = 0; //use long long for avoid overflow
while(x)
{
ans = ans * 10 + x % 10;
x /= 10;
}
if(x < 0) ans = ans * (-1);
return ans > INT_MAX || ans < INT_MIN ? 0 : ans;
}
};
```

note :

because the answer maybe greater than 32-bit,so we should use long long insteat of int

A controversial code

```
class Solution {
public:
int reverse(int x) {
int dir = 1;
long long result = 0;
if(x < 0)
{
dir = -1;
x = -1 * x;
}
while(x)
{
result = result * 10 + (x % 10);
x = x / 10;
}
result = result * dir;
return (result < INT_MIN || result > INT_MAX) ? 0 : result;
}
};
```