Reverse Integer

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, return 0. This is a direct digit-by-digit extraction problem with overflow handling.

Input Format

x = signed integer

Output Format

reversed integer or 0 if overflow occurs

Constraints

  • x is a signed 32-bit integer

Examples

Example 1:

Input:

x = 123

Output:

321

Explanation:

123 reversed becomes 321.

Example 2:

Input:

x = -120

Output:

-21

Explanation:

Leading zeroes are dropped and the sign is preserved.

Loading...
Reverse Integer - Math DSA Problem