Palindrome Number

Check if a number is a palindrome without converting it to a string. A palindrome reads the same backward as forward.

Solution

class Solution {
public:
  bool isPalindrome(int x) {
    // Negative numbers and numbers ending with 0 (except 0 itself) cannot be palindrome
    if (x<0 || (x%10 == 0 && x!=0)) {
      return false;
    }
    long long reverse;
    int original = x;
    while (x>0) {
      int digit = x%10;
      reverse = reverse*10 + digit;
      x/=10;
    }
    return reverse==original;
  }
};