LeetCode 9: Palindrome Number Solution in Python Explained

Problem Statement

Section link icon

LeetCode 9, Palindrome Number, is an easy-level challenge where you determine if a given integer x is a palindrome—an integer that reads the same forward and backward. For example, 121 is a palindrome because it’s 121 both ways, but 123 isn’t. The twist? You need to solve this without converting the number to a string. The goal is to return True if it’s a palindrome and False if it’s not, all while keeping it within 32-bit integer bounds.

Constraints

  • -2³¹ <= x <= 2³¹ - 1: Input is a 32-bit signed integer (-2,147,483,648 to 2,147,483,647).
  • No string conversion allowed (a common interview restriction).

Example

Input: x = 121
Output: true
Explanation: 121 reads as 121 backward, so it’s a palindrome.

Input: x = -121
Output: false
Explanation: -121 backward is 121-, not -121, so it’s not a palindrome.

Input: x = 10
Output: false
Explanation: 10 backward is 01, which isn’t 10, so not a palindrome.

Input: x = 0
Output: true
Explanation: 0 is the same backward, so it’s a palindrome.

Understanding the Problem

Section link icon

How do you solve LeetCode 9: Palindrome Number in Python without strings? A palindrome number mirrors itself—like 12321 or 44. We need to check if the digits, read from left to right, match when read from right to left. Since we can’t use strings, we’ll use math to reverse the number and compare it to the original. Negative numbers aren’t palindromes here because the sign messes up the mirror effect. We’ll use a mathematical approach to tackle this efficiently.

Solution: Mathematical Approach

Section link icon

Explanation

This solution reverses the number by pulling off digits one by one and building a new number, then compares it to the original. It’s like flipping the digits manually without writing them down as text.

Here’s a quick visual for 121:

Original: 121 → Reverse: 1 → 12 → 121 → Compare: 121 == 121 (True)
  1. Handle Edge Cases.
  • If the number is negative or ends in 0 (but isn’t 0), it can’t be a palindrome—return False.
  1. Reverse the Number.
  • Use division and modulo to extract digits from right to left, building the reversed number.
  1. Compare.
  • Check if the reversed number equals the original.
  1. Return Result.
  • Return True if they match, False if they don’t.

Step-by-Step Example

Example 1: x = 121

We have 121 and want to check if it’s a palindrome.

  • Goal: See if 121 reads the same backward.
  • Result for reference: 121 backward is 121, so True.
  • Start: Set reversed number to 0.
  • Step 1: Check edge cases.
    • 121 isn’t negative and doesn’t end in 0 (unless it’s just 0), so keep going.
  • Step 2: Get the last digit (1).
    • 121 ÷ 10 = 12 remainder 1 (121 % 10 = 1).
    • Reversed = 0 * 10 + 1 = 1.
    • Number = 121 ÷ 10 = 12 (integer division).
  • Step 3: Next digit (2).
    • 12 ÷ 10 = 1 remainder 2 (12 % 10 = 2).
    • Reversed = 1 * 10 + 2 = 12.
    • Number = 12 ÷ 10 = 1.
  • Step 4: Last digit (1).
    • 1 ÷ 10 = 0 remainder 1 (1 % 10 = 1).
    • Reversed = 12 * 10 + 1 = 121.
    • Number = 1 ÷ 10 = 0.
  • Step 5: Number is 0, stop reversing.
    • Reversed is 121.
  • Step 6: Compare.
    • Original 121 equals reversed 121, so it’s a palindrome.
  • Finish: Return True.
    • 121 is indeed a palindrome!

Example 2: x = -121

Now, the number is -121.

  • Goal: Check if -121 is a palindrome.
  • Result for reference: -121 backward isn’t -121 (it’s 121-), so False.
  • Start: Reversed at 0.
  • Step 1: Check edge cases.
    • -121 is negative, stop here.
  • Step 2: No need to reverse.
    • Negative numbers can’t be palindromes in this problem.
  • Finish: Return False.
    • The sign ruins the mirror effect.

Example 3: x = 10

For 10, let’s see if it’s a palindrome.

  • Goal: Test if 10 reads the same backward.
  • Result for reference: 10 backward is 01, not 10, so False.
  • Start: Reversed at 0.
  • Step 1: Check edge cases.
    • 10 isn’t negative, but it ends in 0 and isn’t 0 itself, stop here.
  • Step 2: No reversing needed.
    • Numbers ending in 0 (except 0) can’t be palindromes—they’d start with 0 when reversed.
  • Finish: Return False.
    • 10 isn’t a palindrome.

Code

Here’s the Python code to solve LeetCode 9: Palindrome Number. It’s clean and works in Python 3.6+. Want to test it? Try it on Replit!

def isPalindrome(x: int) -> bool:
    # Edge cases: negative or ends in 0 (but not 0 itself)
    if x < 0 or (x % 10 == 0 and x != 0):
        return False

    reversed_half = 0
    # Reverse only half to avoid overflow
    while x > reversed_half:
        digit = x % 10
        reversed_half = reversed_half * 10 + digit
        x //= 10

    # For even length, x == reversed_half; for odd, x == reversed_half // 10
    return x == reversed_half or x == reversed_half // 10

Note: Learn more about Python’s modulo operator in the official docs.

Complexity

  • Time Complexity: O(log₁₀ x) – We process each digit, and the number of digits is logarithmic.
  • Space Complexity: O(1) – Just a couple of variables, no extra space.

Efficiency Notes

This solution is fast and clever. It only reverses half the number to avoid overflow issues, making it efficient for 32-bit integers. It’s a top way to solve LeetCode 9 in Python without strings.

Tips for Success

Here are handy tips to ace LeetCode 9:

  • Catch Negatives Early: Negative numbers are never palindromes here—check this first to save time.
  • Watch the Zero Trap: Numbers ending in 0 (like 10, 100) aren’t palindromes unless it’s just 0—don’t miss this rule.
  • Halfway Trick: Reversing only half the number keeps it simple and avoids big-number problems.

These tips can make your solution foolproof!

Alternative: Full Reverse (Brief Mention)

Section link icon

Explanation

You could fully reverse the number (like in LeetCode 7) and compare it, but that risks overflow for large numbers (e.g., 2147483647). The half-reverse method is safer and preferred, so we focus on that.

Additional Example

Section link icon

For x = 12321:

  • Goal: Check if it’s a palindrome.
  • Reference: 12321 backward is 12321, so True.
  • Steps:
    • Not negative, doesn’t end in 0.
    • Reverse half: 1 → 12 → 123, original becomes 12.
    • Compare: 12 == 123 // 10 (12), matches.
  • Result: Return True.

Edge Cases

Section link icon
  • Negative: x = -1 – Returns False.
  • Zero: x = 0 – Returns True.
  • Trailing Zeros: x = 100 – Returns False.
  • Max Int: x = 2147483647 – Half-reverse avoids overflow, returns False.

Final Thoughts

Section link icon

The mathematical half-reverse approach is a smart, efficient way to solve LeetCode 9: Palindrome Number in Python. It’s perfect for interviews and teaches you to work with numbers creatively.