码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode "Partition List"

时间:2014-08-03 07:50:15      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   art   ar   div   amp   

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;
    }
};

LeetCode "Partition List",布布扣,bubuko.com

LeetCode "Partition List"

标签:style   blog   color   io   art   ar   div   amp   

原文地址:http://www.cnblogs.com/tonix/p/3888009.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!