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

For example,
Given1->2->3->3->4->4->5, return1->2->5.
Given1->1->1->2->3, return2->3.

这题和Remove duplicate from sorted list的区别在于,本题中,只要结点只要出现重复则该值相等的结点都要删除,上题中留一个不删。

思路:这里有可能有修改表头(如:1->1->1>2>3),一般修改表头的题目都会需要一个辅助指针,所以要新建一个结点。遍历链表,遇到相等的相邻结点,直接继续遍历;遇到不相等的两相邻结点时,若pre->next=cur说明cur没有重复的,pre=pre->next即可,若是不等于说明,可能有重复,则,pre连接cur但是pre不移动,需重新进入循环检验是否没有重复(没有重复时,pre->next=cur),直到没有重复结点。源代码

主要的思想是:保持前指针pre不动,因为pre没有重复,所以用其next去和后面的比较。当遇到值不相等时,判断中间是否有重复,即pre->next=cur(这里判断cur是否为pre的后缀,不是值的判断),没有,pre移动,有,则pre不动,但和后面的连接起来,继续重复。

 class Solution {
public:
ListNode *deleteDuplicates(ListNode *head)
{
if(head==NULL) return head; ListNode *newList=new ListNode(-);
newList->next=head;
ListNode *pre=newList;
ListNode *cur=head; while(cur)
{
while(cur->next&&pre->next->val==cur->next->val)
{
cur=cur->next;
} if(pre->next==cur)    //想明白!不是值。
pre=pre->next;
else
{
pre->next=cur->next;
} cur=cur->next;
}
return newList->next;
}
};

[Leetcode] Remove duplicate from sorted list ii 从已排序的链表中删除重复结点的更多相关文章

  1. [Leetcode] Remove duplicates from sorted array ii 从已排序的数组中删除重复元素

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  2. [Leetcode] Remove duplicates from sorted list 从已排序的链表中删除重复元素

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  3. leetcode 题解:Remove Duplicates from Sorted Array II(已排序数组去三次及以上重复元素)

    题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...

  4. [Leetcode] Remove duplicates from sorted array 从已排序的数组中删除重复元素

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

  5. Leetcode: Remove Duplicates from Sorted List II 解题报告

    Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...

  6. [LeetCode] 83. Remove Duplicates from Sorted List ☆(从有序链表中删除重复项)

    描述 Given a sorted linked list, delete all duplicates such that each element appear only once. Exampl ...

  7. LeetCode 82,考察你的基本功,在有序链表中删除重复元素II

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是LeetCode专题的第51篇文章,我们来看LeetCode第82题,删除有序链表中的重复元素II(Remove Duplicates ...

  8. [LeetCode] Remove Duplicates from Sorted List II 移除有序链表中的重复项之二

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

  9. [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

随机推荐

  1. 【php】php 生僻知识点认知

    资料引用来源:http://www.runoob.com/php/php-tutorial.html ​ 表单提交中, 变量名中的点和空格被转换成下划线.例如 <input name=" ...

  2. Hive初识(一)

    LOAD DATA语句 一般来说,在SQL创建表后,我们就可以使用INSERT语句插入数据.但在Hive中,可以使用LOAD DATA语句来插入数据. LOAD DATA [LOCAL] INPATH ...

  3. 网站mysql防止sql注入攻击 3种方法总结

    mysql数据库一直以来都遭受到sql注入攻击的影响,很多网站,包括目前的PC端以及手机端都在使用php+mysql数据库这种架构,大多数网站受到的攻击都是与sql注入攻击有关,那么mysql数据库如 ...

  4. 初识python 字符串 列表 字典相关操作

    python基础(一): 运算符: 算术运算: 除了基本的+ - * / 以外,还需要知道 :  // 为取整除 返回的市商的整数部分 例如: 9 // 2  ---> 4  , 9.0 //  ...

  5. 50条大牛C++编程开发学习建议

    每个从事C++开发的朋友相信都能给后来者一些建议,但是真正为此进行大致总结的很少.本文就给出了网上流传的对C++编程开发学习的50条建议,总结的还是相当不错的,编程学习者(不仅限于C++学习者)如果真 ...

  6. go学习笔记-流程控制(if/else,for/range)

    流程控制(if/else,for/range) if if条件判断语句的语法概括起来就是:如果满足条件就做某事,否则做另一件事. func testIf() { num := 10 if num &l ...

  7. 6 wireshark 安装使用 数据包抓取

    1.wireshark安装 2.开始使用 3.界面详情 4. 数据包抓取 5.过滤数据

  8. 步骤1:JMeter 录制脚本接口测试

    JMeter 常用测试方法简介 1.下载安装 http://jmeter.apache.org/download_jmeter.cgi 安装JDK,配置环境变量JAVA_HOME. 系统要求:JMet ...

  9. jmeter☞文件目录(一)

    Jmeter的文件目录如下图: 1.bin:可执行文件目录 a.jmeter.bat:Windows环境下的启动文件 b.jmeter.log:日志文件 c.jmeter.sh:Linux环境下的启动 ...

  10. LINQ学习笔记——(1)添加扩展方法

    目的:  对已存在类型的行为进行扩展 注意事项:    扩展方法是一种特殊的静态方法    扩展方法必须在静态类中定义    扩展方法的优先级低于同名的类方法    扩展方法只在特定的命名空间内有效 ...