博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Remove Duplicates from Sorted List II
阅读量:6905 次
发布时间:2019-06-27

本文共 1141 字,大约阅读时间需要 3 分钟。

Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.

For example,

Given 1->2->3->3->4->4->5, return 1->2->5.
Given 1->1->1->2->3, return 2->3.

 

Code:

 

class Solution {public:    ListNode *deleteDuplicates(ListNode *head) {        if(head==NULL) return head;        ListNode *start=new ListNode(0);        start->next=head;        ListNode *pre=start;        ListNode *cur=head;        ListNode *nex=head->next;        bool detect=false;        while(nex){            if(nex->val==cur->val){                detect=true;                cur->next=nex->next;                delete nex;            }            else if(detect){                detect=false;                pre->next=cur->next;                delete cur;                cur=nex;            }            else{                pre=cur;                cur=nex;            }            nex=nex->next;        }        if(detect){            pre->next=NULL;            delete cur;        }        head=start->next;        delete start;        return head;    }};

 

转载于:https://www.cnblogs.com/winscoder/p/3408996.html

你可能感兴趣的文章
Lucene介绍和创建索引和搜索初步
查看>>
LNMP的基础上搭建wordpress
查看>>
祝福你的旅行
查看>>
Vsftp搭建(一)与PAM验证 SSL加密 登陆
查看>>
Ubuntu Server 18.04 配置 rsync(xinetd)
查看>>
ClassNotFoundException 和 NoClassDefFoundError 区别
查看>>
特征提取概述
查看>>
python 遇到NameError: name '__file__' is not defi...
查看>>
CentOS7.3 64位安装Hyperledger fabric多通道多组织多节点
查看>>
应用偶发性连接不上Oracle数据库的排查案例
查看>>
CentOS7切换图形启动模式
查看>>
利用python分析日志生成图表
查看>>
c#中不定长参数(关键字Params)使用
查看>>
WinAPI: waveOutPause - 暂停播放
查看>>
FTP自动上传
查看>>
我的友情链接
查看>>
mysqldump工具
查看>>
用 PHP 读取文件的正确方法
查看>>
LoadRunner压力测试时监控服务器Linux的资源情况
查看>>
azure存储并发写 压力测试
查看>>