Reverse digits of an integer.

Example1: x = 123, return 321

Example2: x = -123, return -321

click to show spoilers.

Note:

The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows.

class Solution {

public int reverse(int x) {

int sum = 0;

int factor = x > 0 ? 1 : -1;

x = x < 0 ? x * factor : x;

int prev = 0;

```
while (x > 0)
{
prev = sum;
sum = sum * 10 + (x%10);
if ((sum-(x%10)) / 10 != prev)
{
return 0;
}
x = x/10;
}
return sum * factor;
}
```

}