Here is the better solution if to be written in scala :

```
object Solution {
def reverse(x: Int): Int = {
def reverset(x : Int, acc : Int =0):Int = {
if(x == 0)
acc
else{
if(Math.abs(Int.MaxValue / 10) < Math.abs(acc)) 0 else
reverset(x/10,acc*10+(x%10))
}
}
reverset(x)
}
}
```