The key point is to decide whether the intermediate reversed value has already overflowed, it can be checked by calculating back to the original value by reversing the formula, if calculating back to the original value is unsuccessful, then it is overflowed.

int reverse(int x) {

int tmp=0; int reversed_x=0;

int is_negative=0;

if(x<0){

x=x*(-1);

is_negative=1;

}

```
while(1)
{
tmp=reversed_x*10+x%10;
if((tmp-x%10)/10 != reversed_x) /*check whether result has already overflowed*/
return 0;
else
reversed_x=tmp;
x=x/10;
if(x==0)
break;
}
if(is_negative)
return reversed_x*(-1);
else
return reversed_x;
```

}