Try it first — give it 15 minutes
0/4 revealedStuck? Take one hint at a time. The struggle is where the pattern sticks.
The problem in plain English
You get an integer x, which can be anywhere in the 32-bit range (about −2.1 billion to +2.1 billion). Return true if it reads the same from left to right as from right to left, and false otherwise.
Examples: 121 → true. -121 → false, because backwards it reads 121-. 10 → false, because backwards it reads 01.
Start with brute force
Turn x into a string and compare it with its reverse: str(x) == str(x)[::-1]. It's correct and fast, but LeetCode's follow-up asks you to do it without a string, and interviewers often do too. Working with digits directly is a skill you'll reuse in Plus One.
Building the intuition
How do you get at the digits of a number without a string? Try it on 123:
123 % 10is3. The remainder after dividing by 10 is always the last digit.123 / 10is12in integer division. Dividing by 10 removes the last digit.
Repeat and you get 3, 2, 1: the digits right to left, which is exactly the reversed order. Now build it up. Start with rev = 0, and for each digit do rev = rev * 10 + digit. Multiplying by 10 shifts everything one place left to make room for the new digit: 0 → 3 → 32 → 321. A palindrome is a number that equals its own reverse.
The optimal approach
- If
x < 0, returnfalse. The minus sign can't appear at the end. - Save
original = xand setrev = 0(short for "reversed"). - While
x > 0:rev = rev * 10 + x % 10, thenx = x / 10(integer division). - Return
rev == original.
The subtle detail is overflow. 2147483647 fits in an int, but its reverse, 7463847412, doesn't. In C++ and Java, store rev in a 64-bit long long / long. Python and JavaScript don't have this problem at this size.
The code
Every solution below is tested against the examples and edge cases before it ships. Pick your language — we'll remember it.
class Solution:
def isPalindrome(self, x: int) -> bool:
if x < 0: # "-121" backwards is "121-": never a palindrome
return False
original = x
rev = 0 # the digits of x, read right to left
while x > 0:
digit = x % 10 # last digit
rev = rev * 10 + digit # shift left, append the digit
x //= 10 # chop the last digit off
return rev == originalclass Solution {
public:
bool isPalindrome(int x) {
if (x < 0) { // "-121" backwards is "121-": never a palindrome
return false;
}
int original = x;
long long rev = 0; // 64 bits: reversing 2147483647 gives 7463847412
while (x > 0) {
int digit = x % 10; // last digit
rev = rev * 10 + digit; // shift left, append the digit
x /= 10; // chop the last digit off
}
return rev == original;
}
};class Solution {
public boolean isPalindrome(int x) {
if (x < 0) { // "-121" backwards is "121-": never a palindrome
return false;
}
int original = x;
long rev = 0; // 64 bits: reversing 2147483647 gives 7463847412
while (x > 0) {
int digit = x % 10; // last digit
rev = rev * 10 + digit; // shift left, append the digit
x /= 10; // chop the last digit off
}
return rev == original;
}
}/**
* @param {number} x
* @return {boolean}
*/
var isPalindrome = function (x) {
if (x < 0) { // "-121" backwards is "121-": never a palindrome
return false;
}
const original = x;
let rev = 0; // JS numbers are exact up to 2^53, so no overflow here
while (x > 0) {
const digit = x % 10; // last digit
rev = rev * 10 + digit; // shift left, append the digit
x = Math.floor(x / 10); // chop the last digit off (plain / would give decimals)
}
return rev === original;
};Dry run
x = 121
| x before | digit = x % 10 | rev after | x after |
|---|---|---|---|
| 121 | 1 | 1 | 12 |
| 12 | 2 | 12 | 1 |
| 1 | 1 | 121 | 0 |
x is now 0, so we stop. rev = 121 equals original = 121, so the answer is true.
Complexity
- Time: O(log x). We do one step per digit, and a number
xhas about log₁₀(x) digits. At most 10 steps here. - Space: O(1). Three integer variables.
Edge cases and traps
- Negative numbers. Always
false. Check it explicitly rather than relying on the loop. - Zero. A palindrome: the loop doesn't run and
0 == 0. - Trailing zeros.
10reverses to1, so it's correctlyfalse. - Overflow in C++/Java. An
intreversed can exceed 2³¹ − 1. Use 64 bits. /in Python and JavaScript. Python needs//for integer division. JavaScript needsMath.floor(x / 10).
Say it in the interview
"Negatives can't be palindromes, so I return false. Otherwise I reverse the number with maths: mod 10 gives the last digit, dividing by 10 removes it, and rev times 10 plus the digit appends it. Then I compare. O(log x) time, O(1) space, and a 64-bit reverse so it can't overflow."
Follow-ups interviewers ask
- "What if you can't use a 64-bit type?" Reverse only half the digits. Stop when
rev >= x; then comparex == rev(even length) orx == rev / 10(odd length, the middle digit doesn't matter). Half a number's digits can't overflow. Reject numbers ending in 0 first, except 0 itself.
Practise the same idea
This is Day 3 of a free 90-day plan 🐸
90 problems in the order that builds on itself, one a day, with a walkthrough like this for each. Tick them off and keep your streak.
Get the full roadmap — free