Problem: Leet Code 9 - Palindrome Number


QUESTION:
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.
Example 3:
Input: x = 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
Constraints:
-2<sup>31</sup> <= x <= 2<sup>31</sup> - 1
Follow up: Could you solve it without converting the integer to a string?
💭Analyzing question:
Concept of palindrome:
The given input on reversing results in the same input again when compared is said to be a palindrome
Concept of Number Palindrome:
To reverse the given number and check whether the reverse is same as the given number, if same we say it as palindrome else not a palindrome.Concept of operators:
Modulo Operator (%) - Gives remainder on dividing
Division Operator ( / ) - Gives quotient on dividing
💡Approach:
To first get number input from user
Store it in a temporary variable as modification will be made to the given input directly
To reverse number using while loop, the clear-cut methodology is attached below to understand best of it.
This is how in loop number gets reversed and the output is checked based on the return type in case of function, else using conditional statement too.
We finally compare and return the nature of the given number.
😮💨ATTACHED DETAILED WORKING:
💻My JAVA Code:
class Solution {
public boolean isPalindrome(int x) {
int org = x;
int rev = 0;
while(x > 0){
int rem = x % 10;
rev=rev*10+rem;
x /= 10;
}
return org==rev;
}
}
🖥️Output:
⏱️Efficiency of my approach:
Time Complexity: O(log x); where x denotes the number of digits in the number
Space Complexity: O(1)
🧠My Learnings:
Recap on the fundamental of Operators
Insights on the logic of reversing a number
Tags:
#Java #Leetcode #ProblemSolving #DSA
Subscribe to my newsletter
Read articles from ARCHANA GURUSAMY directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

ARCHANA GURUSAMY
ARCHANA GURUSAMY
👩💻 I'm Archana Gurusamy, a passionate 3rd-year B.Tech IT student on a mission to grow as a developer.I'm currently deep-diving into Data Structures & Algorithms using Java, while also exploring web development and real-world projects. I love solving coding challenges, building meaningful applications, and documenting my learning journey through technical blogs. I believe in learning in public — sharing my logic, code, and even my rough ideas as I grow. I'm actively working on building a strong IT profile while preparing for real world challenges.