Leetcode: Reverse Integer

Input: x = 123
Output: 321
Input: x = -123
Output: -321
class Solution(object):
def reverse(self, x):
flag = 0
list1 =[]
if x < 0:
flag = 1

list1[:0]= str(abs(x))
result = int("".join(list1[::-1]))
if flag == 1:
result = result * -1
if result > 2**31 - 1 or result <= -2**31:
return 0
else:
return(result)

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store