Problem: Leet Code 7 - Reverse Integer
Grasp this straightforward technique to see if a reversed number fits the set limits

👩💻 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.
QUESTION:
Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-2<sup>31</sup>, 2<sup>31</sup> - 1], then return 0.
Assume the environment does not allow you to store 64-bit integers (signed or unsigned).
Example 1:
Input: x = 123
Output: 321
Example 2:
Input: x = -123
Output: -321
Example 3:
Input: x = 120
Output: 21
Constraints:
-2<sup>31</sup> <= x <= 2<sup>31</sup> - 1
💭Analyzing question
Given an integer, we have to reverse it which sounds great when reading the question initially.
But further there is an important constraint that, if on reverse the number is greater tahn 32-bit integer then return 0 instead.
💡Approach:
Now thinking of reverse its like printing numbers from last to first, how can we do it by making use of the Java programming language is use typical loop concept to store numbers from last in separate variable.
Use Modulus operator we find the remainder that is last digit for every time we reduce number by using Division operator, in while loop and we make sure the reduced number doesn’t reach nil.
This is the standard procedure we follow, but here we check the constraint by adding and if statement to check whether current reverse stored in “rev” is already large or not before multiplying with 10.
That’s why we check whether “rev” is greater than the maximum unsigned integer and also less than minimum signed integer, if this condition is true then we return 0 immediately as this violates.
💻My Java Code:
Optimal Approach
class Solution {
public int reverse(int x) {
int rev=0;
while(x!=0){
int rem = x%10;
if(rev>Integer.MAX_VALUE/10 || rev<Integer.MIN_VALUE/10){
return 0;
}
rev = rev*10+rem ;
x/=10;
}
return rev;
}
}
🖥️Output:

⏱️Efficiency of my Approach
Time Complexity: O(n)
Space Complexity: O(1)
🧠My Learnings:
Using built-in functions like MAX and MIN to check violating condition
Refreshing the concept of the reverse number by modulo and division logic
Tags:
#Java #Leetcode #ProblemSolving #DSA



