Simple(10 lines) JAVA solution without reversing list O(n) time and O(1) space


  • 0
    M
        public boolean isPalindrome(ListNode head) {
            return util(head, new ListNode[]{ head } );
        }
        
        private boolean util(ListNode h, ListNode[] rh){        
            if(h == null){ return true; }        
            if(!util(h.next, rh)){ return false; }        
            if(h.val != rh[0].val){ return false; }
            rh[0] = rh[0].next;
            
            return true;
        }
    

Log in to reply
 

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