Reverse Integer

Easy~10 min

Given an integer n, reverse its digits and return the result.

  • If n is negative, the reversed number should also be negative.
  • Leading zeros in the reversed result should be dropped (e.g., 120 becomes 21).

For example, 123 becomes 321, and -456 becomes -654.

Examples

Example 1
Input: n = 123
Output: 321
Explanation: The digits 1, 2, 3 are reversed to 3, 2, 1.
Example 2
Input: n = -456
Output: -654
Explanation: The sign is preserved. The digits 4, 5, 6 are reversed to 6, 5, 4.
Example 3
Input: n = 120
Output: 21
Explanation: The trailing zero in 120 becomes a leading zero in 021, which is dropped.

Constraints

  • -10^9 <= n <= 10^9
  • The result is guaranteed to fit within a 32-bit signed integer.
Code
Ctrl+EnterRun|Ctrl+⇧+EnterSubmit
Output

Run your code to see results

Use Cmd+Enter to run