public int reverse(int x) {
int res = 0;
boolean flag = x > 0;
while(x!=0){
if(Math.abs(res)>214748364Math.abs(res)==214748364&&Math.abs(x%10)>((flag)?7:8)) return 0;
res = res*10+x%10;
x/=10;
}
return res;
}
Short java solution

@gjhawar Actually I was over thinking. Since input x is an int, the situation Math.abs(res)==214748364&&Math.abs(x%10)>((flag)?7:8) will not possibly happen. So the flag is also removeable