Skip to content

Palindrome Number

Given an integer x, return true if x is a palindrome, and false otherwise.

A palindrome reads the same forwards and backwards.

InputOutputExplanation
121trueReads as 121 from left to right and right to left
-121falseReads as -121 forward but 121- backward
10falseReads as 01 from right to left
  • -2^31 <= x <= 2^31 - 1
ApproachTimeSpaceNotes
Without String (math)O(log n)O(1)n = number of digits; no string allocation
With StringO(n)O(n)Converts integer to string; straightforward but uses extra memory
  • Pick Without String if you want the strongest interview answer.
  • Pick With String if you want the easiest version to understand before optimizing.
★ Recommended

Reverse the entire integer mathematically and compare it to the original. Early-exit for negative numbers and numbers ending in zero (except 0 itself).

Think of this as rebuilding the number from right to left. If the rebuilt number matches the original one, the digits were symmetrical.

⏱ Time O(log n) One pass per digit 💾 Space O(1) Integer vars only

Input: x = 121

IterationoriginalXlastDigitnumReversed
Start1210
11211
21212
301121

121 == 121true

Input: x = -121

Negative → return false immediately ✓

Input: x = 10

10 % 10 == 0 (ends in zero, non-zero) → return false immediately ✓

Animated walkthrough

How the math-based palindrome check works

Use Next or Autoplay to watch digits move from the original number into the reversed number until both can be compared.

Step 1 of 5 Target: Compare original and reversed
Waiting to begin...
Array state
Memory / lookup state

function isPalindrome(x):
if x < 0:
return false
if x == 0:
return true
if x % 10 == 0:
return false
original = x
reversed = 0
while x > 0:
lastDigit = x % 10
reversed = reversed * 10 + lastDigit
x = x / 10
return original == reversed
palindrome_number_without_string.py
def is_palindrome(x: int) -> bool:
if x < 0:
return False
if x == 0:
return True
if x % 10 == 0:
return False
num_reversed = 0
original_x = x
while original_x > 0:
last_digit = original_x % 10
num_reversed = num_reversed * 10 + last_digit
original_x //= 10
return x == num_reversed
print(is_palindrome(121)) # True
print(is_palindrome(-121)) # False
print(is_palindrome(10)) # False
✓ Simple

Convert the integer to a string and compare it to its reverse.

This version is easy to read because it turns the problem into a string comparison. The trade-off is extra memory.

⏱ Time O(n) Per digit 💾 Space O(n) String storage

Input: x = 121

str(121)"121" → reversed → "121" → equal → true

Input: x = -121

str(-121)"-121" → reversed → "121-" → not equal → false

Input: x = 10

str(10)"10" → reversed → "01" → not equal → false

function isPalindrome(x):
str_x = str(x)
return str_x == reverse(str_x)
palindrome_number_with_string.py
def is_palindrome(x: int) -> bool:
str_x = str(x)
return str_x == str_x[::-1]
print(is_palindrome(121)) # True
print(is_palindrome(-121)) # False
print(is_palindrome(10)) # False
✓ Simple

A third approach demonstrating an alternative technique for solving this problem. This implementation optimizes either time or space complexity compared to the previous approaches.

⏱ Time O(n) Optimized complexity 💾 Space O(1) Efficient space usage
function approach3():
// Implementation approach goes here
palindrome_number_optimized.py
"""
Solution for Palindrome Number
"""
def solve():
"""Implementation for approach 3 of Palindrome Number"""
pass
if __name__ == "__main__":
print("Solution ready")