LeetCode Solutions
#9 — Palindrome Number
From string trick to half‑reversal — clean integer logic
Problem statement
Given an integer
x, returntrueifxis a palindrome, andfalseotherwise.Example 1:
Example 2:
Example 3:
Constraints:
-2³¹ <= x <= 2³¹ - 1Follow up: Could you solve it without converting the integer to a string?
The naive way
The most straightforward solution is to convert the integer to a string and check if it reads the same backward. It’s short, readable, and passes all test cases easily — but it uses extra memory and avoids the real challenge.
def isPalindrome_string(x: int) -> bool:
s = str(x)
return s == s[::-1]
Initial complexity
Time: O(n), where n is the number of digits (≈ log₁₀(x)).
Space: O(n) for the string representation.
This is acceptable given the constraints (max 10 digits), but the follow‑up asks for a solution without string conversion — and it’s more interesting to work directly with the number.
Reversing without strings
A numeric approach: reverse the integer and compare with the original. Negative numbers are never palindromes (the minus sign breaks symmetry), and numbers ending with 0 (except 0 itself) can't be palindromes because the reverse would have a leading zero.
The obvious method is to build the reversed number by extracting digits
with % 10 and // 10. But reversing the
entire number can cause overflow in languages with
fixed‑width integers. A smarter approach: reverse only half
of the number and compare it with the other half.
How do we know we've reached half? While the original number x
is greater than the reversed half reversed_half, we keep
moving digits from x to reversed_half.
def isPalindrome(x: int) -> bool:
# negative numbers are not palindromes
# also, if last digit is 0, only 0 itself can be a palindrome
if x < 0 or (x % 10 == 0 and x != 0):
return False
reversed_half = 0
while x > reversed_half:
reversed_half = reversed_half * 10 + x % 10
x //= 10
# For even length: x == reversed_half
# For odd length: x == reversed_half // 10 (middle digit doesn't matter)
return x == reversed_half or x == reversed_half // 10
Let’s trace x = 1221 (even length) and x = 121 (odd length):
| Step | x (remaining) | reversed_half | Condition (x > reversed_half) | Action |
|---|---|---|---|---|
| initial | 1221 | 0 | True | Enter loop |
| 1 | 122 | 1 | True | Continue |
| 2 | 12 | 12 | False (12 > 12 is false) | Exit loop |
x = 12, reversed_half = 12. They are equal → palindrome.
| Step | x (remaining) | reversed_half | Condition | Action |
|---|---|---|---|---|
| initial | 121 | 0 | True | Enter loop |
| 1 | 12 | 1 | True | Continue |
| 2 | 1 | 12 | False (1 > 12 false) | Exit loop |
x = 1, reversed_half = 12. We check x == reversed_half // 10 → 1 == 1 → palindrome.
The trick with odd‑length numbers: the middle digit ends up in the last
position of reversed_half, so we discard it with integer division.
Final complexity
Time complexity: O(log₁₀(x)) — we process roughly half the digits.
Space complexity: O(1) — only a few integer variables, no extra structures.
This solution respects the constraints, avoids overflow, and meets the follow‑up requirement. It’s a clean, mathematical way to check for palindromes.
Sometimes the most elegant answer doesn’t need a single character — just a few digits reversed.
References & further reading
- LeetCode Palindrome Number Problem Page — official problem and discussion.
- Wikipedia: Palindromic number — background and properties.
- Wikipedia: Integer overflow — why half‑reversal is safer.
- Real Python: Reverse Strings in Python — the string approach explained.
Can't get enough from lumænaut? Read this...
- Chapter 2 | Everything I Don't Know About Blogging — the logbook behind the site: domains, SEO, plugins, and learning in public.
- The 8 Algorithm Paradigms — eight mental models you’ll keep reusing across problems, interviews, and production code.
- How the Game Loop Works — the heartbeat of a game frame: update, render, and steady timing in plain language.