首页 > 其他 > 详细

LeetCode "Partition List"

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

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"

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!