Palindrome Number - LeetCode Java Solution

Aman WalkeAman Walke
3 min read

👋 Introduction

When we think about palindromes, we usually think about words like "madam" or "racecar", which read the same forward and backward. But numbers can be palindromes too!

In this blog post, we’ll walk through LeetCode Problem 9: Palindrome Number, discuss different approaches to solve it, and understand the time and space complexities.

🔍 Problem Statement

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

Example 1:

Input: x = 121
Output: true
Explanation: 121 reads as 121 from left to right and from right to left.

Example 2:

Input: x = -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.

→ View on LeetCode

💡 Approach 1: Convert to String (Simple & Easy)

Let’s start with the most intuitive solution:

🔧 Idea:

  1. Convert the number to a string.

  2. Use two pointers to check if the string is a palindrome.

🔢 Code:

public boolean isPalindrome(int x) {
    String str = String.valueOf(x);
    int left = 0, right = str.length() - 1;

    while (left < right) {
        if (str.charAt(left++) != str.charAt(right--)) {
            return false;
        }
    }
    return true;
}

🧮 Time Complexity:

  • O(n) where n is the number of digits.

🧠 Space Complexity:

  • O(n) due to string conversion.

💡Approach 2: No String Conversion (Optimized)

This is the interview-favorite approach: solve it without converting the integer to a string.

🔧 Idea:

  1. Negative numbers are never palindromes.

  2. Any number ending in 0 (except 0 itself) cannot be a palindrome (e.g. 10, 100).

  3. Reverse half of the number and compare it with the other half.

    Example:

    • Original: 1221

    • Reverse half: 12 → compare with remaining 12

    • ✅ Match!

public boolean isPalindrome(int x) {
    // Edge cases
    if (x < 0 || (x % 10 == 0 && x != 0)) {
        return false;
    }

    int reversedHalf = 0;
    while (x > reversedHalf) {
        reversedHalf = reversedHalf * 10 + x % 10;
        x = x / 10;
    }

    // For even and odd digit numbers
    return x == reversedHalf || x == reversedHalf / 10;
}

🔍 Explanation:

  • We reverse digits until the reversed number is equal to or greater than the remaining number.

  • For odd-digit numbers, we remove the middle digit using reversedHalf / 10.

🧮 Time Complexity:

  • O(log₁₀(n)) because we divide x by 10 in each loop iteration.

🧠 Space Complexity:

  • O(1) — No extra space used.

🏁 Final Thoughts

LeetCode Problem 9 is a fantastic example of how simple problems can be optimized beyond the brute-force solution. It’s often asked in interviews to see how candidates handle edge cases and space constraints.

Whether you go for the easy string conversion or the optimal in-place reverse, understanding the logic behind palindromes is key.

0
Subscribe to my newsletter

Read articles from Aman Walke directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Aman Walke
Aman Walke

I'm a tech enthusiast who loves building backend systems that just work — clean, scalable, and efficient. I've worked with microservices, Spring Boot, Azure, and APIs, and I enjoy digging into root causes and making systems better. Whether it's writing clean code, reviewing it, or managing deployments with DevOps tools, I'm always up for the challenge. I like working in collaborative environments where I can learn, share, and grow alongside smart people.