concise java solution beat 94%


  • 0
    W
    public boolean isPalindrome(ListNode head) {
        if(head == null || head.next == null) return true;
        
        ListNode f = head, p = head;
        ListNode e = null; ListNode temp = null;
        while(f != null && f.next != null) {
            f = f.next.next;
            temp = p.next;
            p.next = e;
            e = p;
            p = temp;
        }
        
        ListNode c = p;
        if(f != null) { //odd
            c = p.next;
        }
        
        while(c != null) {
            if(c.val != e.val) return false;
            c = c.next;
            e = e.next;
        }
        
        return true;
    }

Log in to reply
 

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