zoukankan      html  css  js  c++  java
  • leetcode 82 删除排序列表中的重复元素II

    与83类似,不过需要注意去除连续的重复片段的情况,如2 2 3 3这种情况,以及【1,1】这种情况下最终的cur为NULL,因此不能再令cur=cur->next;

    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution {
    public:
        ListNode* deleteDuplicates(ListNode* head) {
            ListNode *pre, *cur, *ahead;
            pre=ahead=new ListNode(-1);
            cur=head;
            while(cur!=NULL){
            //去除连续重复片段
    while(cur!=NULL && cur->next!=NULL && cur->next->val==cur->val){ while(cur->next!=NULL && cur->next->val==cur->val){ cur=cur->next; } cur=cur->next; }
             pre
    ->next=cur; pre=cur; if(cur!=NULL) cur=cur->next;//去除cur==NULL的情况
    } return ahead->next; } };

  • 相关阅读:
    centos6.8升级python3.5.2
    钓鱼
    斯诺登的密码
    模板,堆,小根堆
    哥德巴赫猜想(升级版)
    哥德巴赫猜想
    线性筛素数
    乒乓球
    数的重心模板
    笨小猴
  • 原文地址:https://www.cnblogs.com/joelwang/p/11017699.html
Copyright © 2011-2022 走看看