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

LeetCode "Remove Duplicates from Sorted List II"

时间:2014-08-03 07:51:34      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   io   art   ar   

Corner cases!

bubuko.com,布布扣
class Solution {
public:
    ListNode *deleteDuplicates(ListNode *head) {
        if (!head) return head;
        if (!head->next) return head;
        
        ListNode dum(-1); dum.next = head;
        ListNode *pPre = &dum;
        ListNode *p1 = head;
        int cnt = 0;
        int last = std::numeric_limits<int>::max();
        while (p1)
        {
            //    A new one?
            if (p1->val != last)
            {
                if (cnt > 1)
                {
                    pPre->next = p1;
                    last = p1->val;
                }
                else
                {

                    last = p1->val;
                    while (pPre->next != p1) pPre = pPre->next;                    
                }
                cnt = 1;
            }
            else
            {
                cnt++;
            }
            p1 = p1->next;
        }
        if (cnt > 1)
        {
            if (pPre == &dum) return NULL;
            else
            {
                pPre->next = NULL;
            }
        };
        return dum.next;
    }
};
View Code

LeetCode "Remove Duplicates from Sorted List II",布布扣,bubuko.com

LeetCode "Remove Duplicates from Sorted List II"

标签:style   blog   http   color   os   io   art   ar   

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

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