An issue with the test cases

  • 2

    The following code just get AC which actually cannot handle [A,B,B,B,A] properly. So another test case should be added A.S.A.P.

    bool isPalindrome(struct ListNode* head)
        if(!head || !head->next) return true;
        struct ListNode *slow=head, *fast=head->next->next;
        while(fast && fast->next) //split into two halves while the right part might longer by 1 if the length of the link is odd;
            slow = slow->next;
            fast = fast->next->next;
        fast = slow->next;
        slow->next = NULL;
        struct ListNode *cur, *pre, *next; //reverse the left linked and slow will become the head;
        cur = head->next;
        pre = head;
        pre->next = NULL;
            next = cur->next;
            cur->next = pre;
            pre = cur;
            cur = next;
        if(fast->val != slow->val) fast = fast->next; //if the first node is unequal, move fast forward to the next;
            if(fast->val != slow->val)
                return false;
            fast = fast->next;
            slow = slow->next;
        return slow == NULL;

  • 0

    @LHearen Thanks, just added the test case.

Log in to reply

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