```
class Solution:
def reverse(self, x):
sign = 1 if x > 0 else -1
x = sign*x
output = 0
while x > 0:
last_digit = x%10
output = output*10 + last_digit
x = int(x/10)
output = sign*output
if output <= 2**31-1 and output >= -2**31:
return output
else:
return 0
```