首页 > 其他 > 详细

LeetCode OJ--Remove Duplicates from Sorted List II *

时间:2014-02-15 01:05:38      阅读:292      评论:0      收藏:0      [点我收藏+]

http://oj.leetcode.com/problems/remove-duplicates-from-sorted-list-ii/

处理链表的范例

bubuko.com,布布扣
#include <iostream>
using namespace std;
 
struct ListNode {
     int val;
     ListNode *next;
     ListNode(int x) : val(x), next(NULL) {}
 };
 
class Solution {
public:
    ListNode *deleteDuplicates(ListNode *head) {
        if(head == NULL) return head;

        ListNode dummy(INT_MIN);
        dummy.next = head;
        ListNode *prev = &dummy,*cur = head;
        while(cur!= NULL)
        {
            bool duplicated = false;
            while(cur->next != NULL && cur->val == cur->next->val)
            {
                duplicated = true;
                cur = cur->next;
            }
            if(duplicated)
            {
                cur = cur->next;
                continue;
            }
            prev->next = cur;
            prev = prev->next;
            cur = cur->next;
        }
        prev->next = cur;
        return dummy.next;
    }
};

int main()
{
    ListNode *n1 = new ListNode(1);
    ListNode *n2 = new ListNode(2);
    ListNode *n3 = new ListNode(3);
    ListNode *n4 = new ListNode(3);
    ListNode *n5 = new ListNode(3);
    ListNode *n6 = new ListNode(4);
    ListNode *n7 = new ListNode(4);
    n1->next = n2;
    n2->next = n3;
    n3->next = n4;
    n4->next = n5;
    n5->next = n6;
    n6->next = n7;
    ListNode *ans;
    Solution myS;
    ans = myS.deleteDuplicates(n1);
    return 0;
}
bubuko.com,布布扣

LeetCode OJ--Remove Duplicates from Sorted List II *

原文:http://www.cnblogs.com/qingcheng/p/3549077.html

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