Given a signed 32-bit integer, produce the integer whose decimal digits are written in the opposite order. The sign of the value is preserved, and any trailing zeros of the original number simply vanish once they move to the front.
Because the computing environment can only hold signed 32-bit integers, the reversed value might not fit. If the reversal falls outside the range that a 32-bit signed integer can represent, return 0 instead.
Example 1
Input: x = 456
Output: 654
The digits 4, 5, 6 are emitted in reverse to form 654.
Example 2
Input: x = 1999999999
Output: 0
Reversing yields 9999999991, which exceeds the largest 32-bit signed integer, so the answer is 0.
Constraints
-2^31 <= x <= 2^31 - 1The reversed value must fit in a signed 32-bit integer, otherwise return 0Assume the environment cannot store any integer wider than 32 bitsSee the step-by-step animation, the intuition, and clean code in every language — free, no credit card.
FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.
750+ engineers trained · frontend, backend, AWS & AI