DEV Community

Cover image for Leetcode Reverse Integer - Solution and Video Explaination
shubhsheth
shubhsheth

Posted on

Leetcode Reverse Integer - Solution and Video Explaination

Solution

int reverse(int x) { int reversed = 0; int max = INT_MAX/10; while (x != 0) { if (abs(reversed) > max) { return 0; } reversed *= 10; reversed += (x % 10); x /= 10; } return reversed; } 
Enter fullscreen mode Exit fullscreen mode

Complexity

Runtime: O(n)
Space: O(n)

Top comments (0)