public int reverse(int x) {
        int abs = x < 0 ? -x : x;
        long result = 0;
        while (abs > 0) {
            result = result * 10 + abs % 10;
            abs = abs / 10;
        }
        result = x < 0 ? result * -1 : result;
        if (result > Integer.MAX_VALUE || result < Integer.MIN_VALUE) {
            return 0;
        }
        return (int) result;
    }