26 line C solution (12ms)


  • 3
    R

    I reversed half of the list from the head.

    bool isPalindrome(struct ListNode* head) {
        int cnt=0;
        struct ListNode *p=head, *pm=head, *pn=NULL, *pp=NULL;
    
        while(p){
            p = p->next;
            if(p){
                p = p->next;
                pn = pm->next;
                pm->next = pp;
                pp = pm;
                pm = pn;
                cnt += 2;
            }else{
                cnt++;
                break;
            }
        }
    
        if(cnt<2) return true;
        if(cnt&1) pn = pn->next;
        for(; pp; pp=pp->next, pn=pn->next)
            if(pp->val != pn->val)
                return false;
        return true;
    }

Log in to reply
 

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