public int reverse(int x) {
        boolean isNeg = false;
        if(x < 0) {
            isNeg = true;
            x = -1 * x;
        }
        int res = 0;
        while (x > 0){
            res = 10 * res + x % 10;
            x /= 10;
        }
        if (res < 0){ // probably res > MAX_VALUE
            return 0;
        }else {
            if (isNeg) return -1 * res;
            else return res;
        }
    }