const solve = (nums) => { let left = 0, right = nums.length - 1; while (left < right) { const sum = nums[left] + nums[right]; if (sum === target) return [left, right]; }}
Problem Walkthrough

Palindrome Number LeetCode Solution: Reverse Half the Digits

Palindrome Number (#9) can be solved without converting to string — reversing only half the number is the elegant O(1) space trick that interviewers love to see.

5 min read|

Reverse only half the digits for O(1) space palindrome check

The elegant math trick behind LeetCode #9 that avoids string conversion

Palindrome Number: The Half-Reversal Trick

Palindrome number leetcode (#9) is one of the most-solved Easy problems on the platform, and for good reason. The question is deceptively simple — check whether an integer reads the same forwards and backwards. Most people immediately reach for string conversion, but the follow-up challenge is what makes this problem genuinely interesting: can you solve it without converting to a string?

The answer is yes, and the technique is elegant. Instead of reversing the entire number and risking integer overflow, you reverse only the second half of the digits. When the reversed half matches the remaining first half, you have a palindrome. This approach runs in O(log n) time and O(1) space — no extra strings, no extra arrays.

In this walkthrough, you will learn both the string approach and the optimal half-reversal method for LeetCode 9, including the edge cases that trip up many candidates.

Understanding the Palindrome Number Problem

Given an integer x, return true if x is a palindrome integer — meaning it reads the same forwards and backwards. For example, 121 is a palindrome because reversing it gives 121. The number -121 is not a palindrome because reversing it gives 121-, which is different from the original.

The constraints are straightforward: x ranges from -2^31 to 2^31 - 1. Negative numbers are never palindromes because the minus sign only appears at the front. The follow-up question asks whether you can solve it without converting the integer to a string, which pushes you toward a purely mathematical approach.

This is where the palindrome number explained distinction matters. The naive approach works fine, but the math-based approach demonstrates deeper understanding of number manipulation — exactly what interviewers look for in a candidate.

ℹ️

Did You Know

Palindrome Number (#9) is the 9th LeetCode problem and one of the most solved Easys — the follow-up "can you solve without string conversion?" is what makes it interesting.

Approach 1: String Conversion

The simplest approach is to convert the integer to a string, reverse the string, and compare. In Python this is a one-liner: return str(x) == str(x)[::-1]. In JavaScript, you would convert to a string, split into characters, reverse, join, and compare. This works correctly for all inputs including negatives.

The time complexity is O(n) where n is the number of digits, since you need to process every character. The space complexity is also O(n) because you create a reversed copy of the string. For an Easy problem in an interview, this solution is perfectly acceptable and shows you can solve the problem quickly.

However, the interviewer will almost certainly follow up with the string-free version. String conversion creates extra memory allocations and does not demonstrate understanding of mathematical digit extraction. The check palindrome integer problem becomes more interesting when you constrain yourself to pure arithmetic.

  • Convert integer to string, reverse, compare
  • Time: O(n) where n is the number of digits
  • Space: O(n) for the reversed string copy
  • Simple and correct but uses extra space

Approach 2: Reverse Half the Number (Optimal)

The optimal approach avoids string conversion entirely by using the reverse half number technique. The idea is brilliant in its simplicity: instead of reversing the entire number (which could overflow for large values), you only reverse the second half of the digits. When you have processed enough digits, you compare the two halves.

You start with two variables — x (the original number, which shrinks as you remove digits) and reversed (which grows as you append digits). In each iteration, you pop the last digit from x using x % 10 and push it onto reversed using reversed * 10 + digit. You continue until reversed is greater than or equal to x, meaning you have processed half or more of the digits.

At that point, for an even number of digits, x should equal reversed exactly. For an odd number of digits, x should equal reversed divided by 10 (integer division), because the middle digit ends up in reversed but does not need to match anything. This is palindrome number without string conversion at its most elegant.

💡

Key Insight

Reverse only the SECOND half: pop digits from x and push to reversed until reversed >= x. For even digits, x == reversed. For odd digits, x == reversed // 10 (middle digit doesn't matter).

Implementation Walkthrough

The implementation of the leetcode 9 solution starts with two quick checks. If x is negative, return false immediately — negative numbers cannot be palindromes. If x ends in 0 but is not 0 itself, return false — no palindrome can start with 0. These early returns handle the most common edge cases before the main loop begins.

The main loop is where the magic happens. Initialize reversed to 0. While x is greater than reversed, extract the last digit of x with x % 10, append it to reversed with reversed = reversed * 10 + x % 10, and shrink x with integer division x = Math.floor(x / 10). The loop naturally stops when you have reversed half the digits.

After the loop, return true if x equals reversed (even-length palindrome) or x equals Math.floor(reversed / 10) (odd-length palindrome). The entire solution uses O(log n) time because you process half the digits, and O(1) space because you only use two integer variables.

  1. 1If x < 0, return false (negatives are not palindromes)
  2. 2If x % 10 == 0 and x != 0, return false (trailing zeros)
  3. 3Initialize reversed = 0
  4. 4While x > reversed: reversed = reversed * 10 + x % 10, then x = Math.floor(x / 10)
  5. 5Return x == reversed (even digits) or x == Math.floor(reversed / 10) (odd digits)

Edge Cases and Gotchas

Negative numbers are the simplest edge case — they are never palindromes. The minus sign means the number cannot read the same in both directions. Return false immediately without any further processing.

Numbers ending in zero deserve special attention. The number 10 is not a palindrome because reversing it gives 01, which is just 1. Similarly, 100, 1000, and 120 are not palindromes. The only number that ends in 0 and is a palindrome is 0 itself. Checking this early with x % 10 == 0 && x != 0 prevents the half-reversal loop from producing incorrect results.

Single-digit numbers (0 through 9) are all palindromes. The half-reversal loop handles them correctly because after one iteration, reversed will be greater than or equal to x and the comparison succeeds. Zero is a palindrome, and this is consistent with the trailing-zero check since we explicitly allow x == 0.

  • Negative numbers: always false (-121, -1)
  • Single digits: always true (0 through 9)
  • Trailing zeros: false except 0 itself (10, 100, 120)
  • Even-length palindromes: 1221, 4554 — x == reversed after loop
  • Odd-length palindromes: 12321 — x == reversed // 10 after loop
⚠️

Common Trap

Numbers ending in 0 (like 10, 100) are NOT palindromes (except 0 itself) — check this early: if x % 10 == 0 and x != 0, return false immediately.

What Palindrome Number Teaches You

Palindrome Number (#9) teaches the half-reversal trick — a technique that appears whenever you need to compare two halves of a number or sequence without extra space. The core idea of popping digits with modulo and pushing with multiplication is reusable across problems involving digit manipulation, including Reverse Integer (#7) and Happy Number (#202).

More broadly, this problem illustrates the difference between a brute-force approach and a math-based approach. String conversion works, but the is palindrome integer check using arithmetic shows you understand how numbers are structured at the digit level. Interviewers notice this distinction.

If you are building your pattern library for coding interviews, add Palindrome Number to your math and number manipulation category in YeetCode. Pair it with Valid Palindrome (#125) for the string version and Longest Palindromic Substring (#5) for the expansion technique. Spaced repetition with YeetCode flashcards will keep the half-reversal pattern fresh when you need it most.

Ready to master algorithm patterns?

YeetCode flashcards help you build pattern recognition through active recall and spaced repetition.

Start practicing now