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. Python学习 :格式化输出

    方式一:使用占位符 % 常用占位符:% s   (s = string 字符串)     % d   (d = digit 整数(十进制))   %  f   ( f = float  浮点数) na ...

  2. liunx下搭建python开发环境

    =============================================================================注意: 在linux下安装新的版本的pytho ...

  3. JavaScript之原型 Prototype

    1.我们所创建的每一个函数,解析器都会向函数中添加一个属性prototype.这个属性对应着一个对象,这个对象就是我们所谓的原型对象.如果函索作为普通函数调用prototype没有任何作用. 当函数以 ...

  4. JavaSE基础复习---Class类与反射机制

    ---恢复内容开始--- 目录: 1.java.lang.class类 2.Java中的反射机制 3.运行时与编译时概念 1. java.lang.class类 Java程序在运行时,Java运行时系 ...

  5. node 分层开发

    app.js var express = require('express');var app = express();app.use('/',require('./control'));app.us ...

  6. Java——英文字母---18.10.11

    package lianxi;import java.io.*;import java.util.Scanner;public class file{  public static void main ...

  7. win7 下安装oracle 11g出现错误: 启动服务出现错误 找不到服务OracleMTSRecoveryService

    这种错误是在多次安装oracle都没有成功的情况下发生的. 正确安装oracle,是有前提条件的 1,安装最新的jdk,不是jre!!(并配好环境变量,在cmd中测试 java -version与ja ...

  8. python2.7练习小例子(十八)

    19):题目:一个数如果恰好等于它的因子之和,这个数就称为"完数".例如6=1+2+3.编程找出1000以内的所有完数.      #!/usr/bin/python # -*- ...

  9. python2.7练习小例子(五)

        5):题目:输入三个整数x,y,z,请把这三个数由小到大输出.     程序分析:我们想办法把最小的数放到x上,先将x与y进行比较,如果x>y则将x与y的值进行交换,然后再用x与z进行比 ...

  10. 【转】已有打开的与此 Command 相关联的 DataReader,必须首先将它关闭

    在运用Linq to sql 或者 linq to entity等相关linq技术进行数据库访问操作时,如果发生上述异常是因为是因为.NET內部是使用DataReader作数据存取,DataReade ...