public int reverse(int x) {
        boolean negative = true;
        int limit = Integer.MIN_VALUE;
        if(x > 0) {
            negative = false;
            x = -x;
            limit = -Integer.MAX_VALUE;
        }

        int ret = 0;

        while(x != 0 && x%10 == 0) {
            x/=10;
        }

        while(x != 0) {
            int curVal = x % 10;

            if(ret < limit/10) {
                return 0;
            }

            if(ret*10 < limit-curVal) {
                return 0;
            }

            ret = ret*10 + curVal;

            x/=10;
        }

        return negative?ret:-ret;
    }