Palindrome Number (LeetCode 9): Solution and Explanation

Easy
Math

Given an integer, decide whether it reads the same forward and backward in decimal. Negative numbers never qualify, because the minus sign only appears on one side, and the follow-up asks you to solve it without converting the number to a string.

Variables
No variables to display
DepthFunction Call
Stack empty
0/0

Intuition

The easiest solution compares the string form of the number with its reverse. It is correct and short, but the follow-up rules it out, and it allocates a string for what is really an arithmetic question.

The arithmetic version starts by reversing the whole number digit by digit and comparing the result with the original. That works, but the reversed value of a large 32-bit number can overflow in languages with fixed-width integers. It also does twice as much work as necessary, because a palindrome is determined by its two halves.

The better idea is to reverse only the second half. Keep popping digits off the end of x and pushing them onto reversed_half. Once reversed_half is at least as large as what remains of x, you have processed half the digits. For an even number of digits, the two halves must be equal. For an odd number of digits, the middle digit ended up in reversed_half, so drop it with reversed_half // 10 and compare.

Two quick rejections make the loop condition sound. Negative numbers are never palindromes. And a number ending in 0 cannot be a palindrome unless it is 0 itself, because it would need to start with 0. Without that check, a number like 10 would leave x = 0 and reversed_half = 0 after one step and wrongly pass.

Approach: Reverse half of the digits

  1. Return False if x is negative, or if it ends in 0 and is not 0.
  2. Start with reversed_half = 0.
  3. While x is greater than reversed_half, move the last digit of x onto reversed_half with reversed_half * 10 + x % 10, then drop it from x with x //= 10.
  4. When the loop stops, about half the digits have moved across.
  5. Return True if x == reversed_half (even digit count) or x == reversed_half // 10 (odd digit count, ignoring the middle digit).

Step-by-step example

x = 1221
  1. 1221 is positive and does not end in 0, so the checks pass.
  2. First iteration: reversed_half becomes 1 and x becomes 122.
  3. Second iteration: reversed_half becomes 12 and x becomes 12.
  4. Now x is no longer greater than reversed_half, so the loop stops. The halves are equal, so the function returns True. For 121, the loop would stop at x = 1 and reversed_half = 12, and 12 // 10 = 1 would match.

Code: Python, Java, C++

1class Solution:
2    def isPalindrome(self, x: int) -> bool:
3        if x < 0 or (x % 10 == 0 and x != 0):
4            return False
5
6        reversed_half = 0
7        while x > reversed_half:
8            reversed_half = reversed_half * 10 + x % 10
9            x //= 10
10
11        return x == reversed_half or x == reversed_half // 10

Time and space complexity

Time

O(log x)

Each iteration removes one decimal digit, and the loop stops after about half of the log₁₀ x digits.

Space

O(1)

Only two integers are stored.

Other approaches

String comparison (time O(log x), space O(log x))

Compare str(x) with its reverse. It is the shortest solution and fine in practice, but it uses extra memory and does not satisfy the no-string follow-up.

Full reversal (time O(log x), space O(1))

Reverse every digit into a new number and compare with the original. It is simple and safe in Python, but in fixed-width languages it needs a wider type to avoid overflow on large inputs.

Common mistakes

  • Forgetting the trailing zero check. Without it, 10 returns True because both halves end up as 0.
  • Treating negative numbers as palindromes by taking the absolute value. -121 reads as 121- backward, so it is not a palindrome.
  • Comparing only x == reversed_half and missing odd-length numbers such as 121, where the middle digit must be dropped.
  • Reversing the entire number in Java or C++ with a 32-bit int, which can overflow for inputs close to the limit.

Frequently asked questions

How do you check if a number is a palindrome without converting it to a string?

Reverse the last half of its digits arithmetically with % 10 and // 10, then compare the reversed half with the remaining front half. Drop the middle digit for odd lengths.

Why are negative numbers not palindromes?

The minus sign is part of how the number is written, and it only appears at the front. Read backward, -121 becomes 121-, which is a different string.

Why reverse only half of the number?

It does half the work and avoids overflow, because the half-reversed value can never exceed the original. It also gives a natural stopping point when the reversed part catches up with the remaining part.

What is the time complexity of Palindrome Number?

It is O(log x), since the loop processes about half of the decimal digits, and the space is O(1). For a 32-bit input that means at most five iterations.