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 [-231, 231 - 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:
-231 <= x <= 231 - 1Problem Overview: Reverse the digits of a signed 32-bit integer x. If reversing the digits causes the value to go outside the 32-bit signed integer range [-2^31, 2^31 - 1], return 0. Negative numbers must preserve their sign after reversal.
Approach 1: Iterative Division and Modulus (Time: O(d), Space: O(1))
This approach processes the integer digit by digit using arithmetic operations. Repeatedly extract the last digit using x % 10, append it to a new number using rev = rev * 10 + digit, and remove the last digit from the original number with integer division x //= 10. The key challenge is detecting overflow before it happens. You check whether rev will exceed the 32-bit boundary before multiplying by 10. This solution uses constant space and avoids converting the number to a string, making it the standard arithmetic solution expected in interviews involving math and integer manipulation.
Approach 2: String Conversion and Manipulation (Time: O(d), Space: O(d))
This approach converts the integer to a string, reverses the characters, and converts the result back to an integer. Handle the negative sign separately: store it, reverse the remaining digits, then reapply the sign. After conversion, verify the result still falls within the 32-bit signed integer range. This method is simpler to implement and easier to reason about, but it uses extra memory for the string and relies on built-in string operations from languages such as Python or JavaScript. It demonstrates basic manipulation of strings rather than pure math operations.
Recommended for interviews: The iterative division and modulus approach is the one interviewers expect. It shows you understand integer digit extraction, overflow detection, and constant-space arithmetic. The string method is acceptable for quick implementations, but the arithmetic solution demonstrates stronger algorithmic control over numeric operations.
This method involves extracting digits one at a time from the integer using modulus and division operations. By repeatedly taking the last digit of the number, you can construct the reversed integer. However, special care must be taken to handle the overflow condition, as reversing an integer can potentially produce a value beyond the 32-bit signed integer limit.
This C solution uses a while loop to pop digits from the input number x and push them onto the result. Overflow conditions are checked both before multiplication and addition.
Time Complexity: O(log10(x)) because we are processing each digit once.
Space Complexity: O(1) since we are using constant extra space.
In this approach, the integer is first converted to a string representation, reversed as a string, and then converted back to an integer. This method mitigates overflow issues by checking the integer post-conversion if it falls within the valid range.
This Python solution converts the integer x to a string, reverses it, and then converts it back to an integer. The sign is preserved and checked for overflow before returning the result.
Python
JavaScript
Time Complexity: O(n) where n is the number of digits in x.
Space Complexity: O(n) due to the use of the string to store digits temporarily.
Let's denote mi and mx as -2^{31} and 2^{31} - 1 respectively, then the reverse result of x, ans, needs to satisfy mi \le ans \le mx.
We can continuously take the remainder of x to get the last digit y of x, and add y to the end of ans. Before adding y, we need to check if ans overflows. That is, check whether ans times 10 + y is within the range [mi, mx].
If x \gt 0, it needs to satisfy ans times 10 + y leq mx, that is, ans times 10 + y leq \left \lfloor \frac{mx}{10} \right \rfloor times 10 + 7. Rearranging gives (ans - \left \lfloor \frac{mx}{10} \right \rfloor) times 10 leq 7 - y.
Next, we discuss the conditions for the inequality to hold:
ans \lt \left \lfloor \frac{mx}{10} \right \rfloor, the inequality obviously holds;ans = \left \lfloor \frac{mx}{10} \right \rfloor, the necessary and sufficient condition for the inequality to hold is y leq 7. If ans = \left \lfloor \frac{mx}{10} \right \rfloor and we can still add numbers, it means that the number is at the highest digit, that is, y must not exceed 2, therefore, the inequality must hold;ans \gt \left \lfloor \frac{mx}{10} \right \rfloor, the inequality obviously does not hold.In summary, when x \gt 0, the necessary and sufficient condition for the inequality to hold is ans leq \left \lfloor \frac{mx}{10} \right \rfloor.
Similarly, when x \lt 0, the necessary and sufficient condition for the inequality to hold is ans geq \left \lfloor \frac{mi}{10} \right \rfloor.
Therefore, we can check whether ans overflows by checking whether ans is within the range [\left \lfloor \frac{mi}{10} \right \rfloor, \left \lfloor \frac{mx}{10} \right \rfloor]. If it overflows, return 0. Otherwise, add y to the end of ans, and then remove the last digit of x, that is, x \gets \left \lfloor \frac{x}{10} \right \rfloor.
The time complexity is O(log |x|), where |x| is the absolute value of x. The space complexity is O(1).
| Approach | Complexity |
|---|---|
| Iterative Division and Modulus | Time Complexity: O(log10(x)) because we are processing each digit once. |
| String Conversion and Manipulation | Time Complexity: O(n) where n is the number of digits in x. |
| Mathematics | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Iterative Division and Modulus | O(d) | O(1) | Preferred interview solution. Uses arithmetic operations and handles overflow explicitly. |
| String Conversion and Manipulation | O(d) | O(d) | Quick implementation when readability matters more than strict constant space. |
Reverse Integer - Bit Manipulation - Leetcode 7 - Python • NeetCode • 127,131 views views
Watch 9 more video solutions →Practice Reverse Integer with our built-in code editor and test cases.
Practice on FleetCode