23ms C++ solution beats 100%


  • 0
    X
    class Solution {
    public:
        bool isPalindrome(ListNode* head) {
            ListNode *pfast = head, *pr, *pl, *pt;
            if (!pfast || !(pfast->next)) {
                return true;
            }
            pl = head;
            pr = head->next;
            pfast = pfast->next;
            head->next = NULL;
            while (pfast->next && pfast->next->next) {
                pfast = pfast->next->next;
                pt = pr->next;
                pr->next = pl;
                pl = pr;
                pr = pt;
            }
            pr = pfast->next ? pr->next : pr;
            while (pr) {
                if (pl->val != pr->val) {
                    return false;
                } else {
                    pl = pl->next;
                    pr = pr->next;
                }
            }
            return true;
        }
    };
    

    0_1473041977475_Screen Shot 2016-09-05 at 10.12.02.png


Log in to reply
 

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