7. Reverse integer

#include <limits.h>

int reverse(int x) {
  int res = 0;

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

    if (res > INT_MAX / 10 || res < INT_MIN / 10)
      return 0;
    res = res * 10 + d;
  }

  return res;
}
#include <climits>

using namespace std;

class Solution {
public:
  int reverse(int x) {
    int res = 0;

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

      if (res > INT_MAX / 10 || res < INT_MIN / 10)
        return 0;
      res = res * 10 + d;
    }

    return res;
  }
};
package bitmanipulation

import "math"

func reverse(x int) int {
    res := 0

    for x != 0 {
        d := x % 10
        x = x / 10

        if res > math.MaxInt32/10 || res < math.MinInt32/10 {
            return 0
        }
        res = res*10 + d
    }

    return res
}
class Solution {
    public int reverse(int x) {
        int res = 0;

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

            if (res > Integer.MAX_VALUE / 10 || res < Integer.MIN_VALUE / 10)
                return 0;
            res = res * 10 + d;
        }

        return res;
    }
}
const INT_MAX = 2 ** 31 - 1, INT_MIN = -(2 ** 31);

/**
 * @param {number} x
 * @return {number}
 */
var reverse = function (x) {
  let res = 0;

  while (x !== 0) {
    const d = x % 10;
    x = Math.trunc(x / 10);

    if (res > Math.trunc(INT_MAX / 10) || res < Math.trunc(INT_MIN / 10))
      return 0;
    res = res * 10 + d;
  }

  return res;
};
import math

INT_MAX, INT_MIN = 2**31 - 1, -(2**31)


class Solution:
    def reverse(self, x: int) -> int:
        res = 0

        while x != 0:
            # Python's // and % round toward -inf: use C-style truncation
            d = int(math.fmod(x, 10))
            x = int(x / 10)

            if res > INT_MAX // 10 or res < int(INT_MIN / 10):
                return 0
            res = res * 10 + d

        return res
impl Solution {
    pub fn reverse(mut x: i32) -> i32 {
        let mut res: i32 = 0;

        while x != 0 {
            let d = x % 10;
            x /= 10;

            if res > i32::MAX / 10 || res < i32::MIN / 10 {
                return 0;
            }
            res = res * 10 + d;
        }

        res
    }
}