LeetCode "Partition List"

Nothing special. A typical list manipulation problem.

class Solution {
public:
    ListNode *partition(ListNode *head, int x) {
        if (!head) return NULL;
        if (!head->next) return head;

        ListNode dum(-1); dum.next = head;
        ListNode *pPre = &dum;
        ListNode *plast = &dum;
        ListNode *p = head;
        while (p)
        {
            if (p->val < x)
            {
                if (pPre->next == p)
                {
                    pPre = p;
                }
                else
                {
                    ListNode *pPreNext = pPre->next;
                    //    dis-link
                    plast->next = p->next;
                    //    Move ahead                
                    pPre->next = p;
                    p->next = pPreNext;
                    pPre = p;
                    //
                    p = plast->next;
                    continue;
                }
            }
            plast = p;
            p = plast->next;
        }
        return dum.next;
    }
};
原文地址:https://www.cnblogs.com/tonix/p/3888009.html