5 liner O(n) time O(1) space, NO reverse of the list


  • -1
    I
        public boolean isPalindrome(ListNode head) {
            int lhash = 0, rhash = 0;
            for(int x = 1; head != null; head = head.next, x *= 31) {
                lhash = lhash*31 + head.val;            
                rhash = rhash + head.val*x;
            }        
            return lhash == rhash;
        }
    
    

Log in to reply
 

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