Reverse Integer


  • 0
    P
    class Solution {
        public int reverse(int x) {
    
            int reverse = 0;
            while(x != 0) {
                int tail = x % 10;
                int newNum = (reverse * 10 + tail);
                if((newNum - tail) /10 != reverse) {
                    return 0;
                }
                reverse = newNum;
                x /= 10;
            }
    
            return reverse;
            
        }
    }
    
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.