LeetCode 7 – Reverse Integer

Posted on December 22, 2013

Last updated on December 22, 2013

Solution to LeetCode Reverse Integer problem.

This is a simple implementation question.

public int reverse(int x) {
  boolean neg = (x < 0);
  if(neg) x*= -1;
  int res = 0;
  while(x != 0){
    res *= 10;
    int div = x / 10;
    int mod = x % 10;
    res += mod;
    x = div;
  }
  if(neg) res *= -1;
  return res;
}
Reverse Integer
Markdown SHA1: 09dccc7f5be7b05ac4165fc6a12602ebb087d8d8