--- tags: data_structure_python --- # Reverse Integer <img src="https://img.shields.io/badge/-easy-brightgreen"> Given a 32-bit signed integer, reverse digits of an integer. <ins>**Example 1:**</ins> >Input: 123 >Output: 321 <ins>**Example 2:**</ins> >Input: -123 >Output: -321 <ins>**Example 3:**</ins> >Input: 120 >Output: 21 **Note:** Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: $[−231, 231 − 1]$. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows. ## Solution ```python= class Solution: def reverse(self, x: int) -> int: res = 0 if x < 0: sign = -1 x = -x else: sign = 1 while x != 0: res = 10 * res + (x % 10) if res < -2**31 or res > 2**31 - 1: return 0 x = x // 10 return sign * res ```