首页 > 其他 > 详细

leetcode-Remove Duplicates from Sorted List II

时间:2016-05-24 11:59:30      阅读:135      评论:0      收藏:0      [点我收藏+]

题目链接:https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/

分析:

设置三个指针,pre, cur, next, 然后进行模拟删除操作,比较简单,具体代码如下:

class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        if(head == NULL || head->next == NULL)
        {
            return head;
        }
        ListNode dummy(-1);
        dummy.next = head;
        ListNode *pre = &dummy;
        ListNode *cur = head;
        while(cur != NULL && cur->next != NULL)
        {
            ListNode *next = cur->next;
            if(cur->val != next->val)
            {
                pre = cur;
                cur = cur->next;
            }
            else
            {
                while(next != NULL && cur->val == next->val)
                {
                    cur = cur->next;
                    next = cur->next;
                }
                pre->next = next;
                cur = next;
            }
        }
        return dummy.next;
    }
};

 

leetcode-Remove Duplicates from Sorted List II

原文:http://www.cnblogs.com/shirley-ict/p/5522798.html

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