1 题目

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.

接口

ListNode deleteDuplicates(ListNode head) 

把出现重复的元素全部删除。

2 思路

双指针。把前驱指针指向上一个不重复的元素中,如果找到不重复元素,则把前驱指针知道该元素,否则删除此元素。另一个指针遍历。

细节实现有挺多地方需要注意的:

① 1个while循环条件 pCur != null

② 寻找不重复的元素 while循环条件 pCur.next != null && prev.next.val == pCur.next.val

复杂度

Time: O(n)
Space: O(1)

3 代码

     public ListNode deleteDuplicates(ListNode head) {
if(head == null)
return head;
ListNode dummy = new ListNode(Integer.MAX_VALUE);
dummy.next = head;
ListNode prev = dummy;
ListNode pCur = head;
while (pCur != null)
{
while(pCur.next != null && prev.next.val == pCur.next.val){
pCur = pCur.next;
}
if(prev.next == pCur){ // 找到单独的元素
prev = prev.next;
} else{ // 剔除重复的元素
prev.next = pCur.next;
}
pCur = pCur.next;
}
return dummy.next;
}

4 总结

思路和Remove Duplicates from Sorted List一样,但是细节实现更多。

5 扩展

如何从排序数组中去除重复的元素?

6 参考

  1. leetcode
  2. Code Ganker征服代码
 

lc面试准备:Remove Duplicates from Sorted List II的更多相关文章

  1. Remove Duplicates from Sorted List II

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

  2. 【leetcode】Remove Duplicates from Sorted Array II

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  3. 50. Remove Duplicates from Sorted Array && Remove Duplicates from Sorted Array II && Remove Element

    Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...

  4. 48. Remove Duplicates from Sorted List && Remove Duplicates from Sorted List II

    Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such that each ...

  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. Remove Element,Remove Duplicates from Sorted Array,Remove Duplicates from Sorted Array II

    以下三个问题的典型的两个指针处理数组的问题,一个指针用于遍历,一个指针用于指向当前处理到位置 一:Remove Element Given an array and a value, remove a ...

  7. LeetCode之“链表”:Remove Duplicates from Sorted List && Remove Duplicates from Sorted List II

    1. Remove Duplicates from Sorted List 题目链接 题目要求: Given a sorted linked list, delete all duplicates s ...

  8. LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>

    LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...

  9. leetcode 203. Remove Linked List Elements 、83. Remove Duplicates from Sorted List 、82. Remove Duplicates from Sorted List II(剑指offer57 删除链表中重复的结点)

    203题是在链表中删除一个固定的值,83题是在链表中删除重复的数值,但要保留一个:82也是删除重复的数值,但重复的都删除,不保留. 比如[1.2.2.3],83题要求的结果是[1.2.3],82题要求 ...

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

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

随机推荐

  1. Oracle11g新特性导致空表不能导出问题

        ORACLE 11G在用EXP导出时,发现空表(没有数据或者没有用过的表)不能导出了.     查了一下资料,说是Oracle 11G中有个新特性,当表无数据时,不分配segment,以节省空 ...

  2. 在KALI LINUX中安装JAVA JDK

    1. 下载最新的JAVA JDK jdk-8u91-linux-x64 2. 解压缩文件并移动至/opt tar -xzvf jdk-8u91-linux-x64.tar.gz mv jdk1.8.0 ...

  3. 256MB小内存MySQL配置优化

    [client] port = 3306 socket = /var/run/mysqld/mysqld.sock [mysqld_safe] socket = /var/run/mysqld/mys ...

  4. .net+easyui系列--datagrid

    加载CSS <link href="../../Public/easyui/SiteEasy.css" rel="stylesheet" type=&qu ...

  5. Mysql INNER,LEFT ,RIGHT join的使用

    JOIN 按照功能大致分为如下三类: INNER JOIN(内连接,或等值连接):获取两个表中字段匹配关系的记录. LEFT JOIN(左连接):获取左表所有记录,即使右表没有对应匹配的记录. RIG ...

  6. C#将汉字转换成全拼的拼音EcanConvertToCh帮助类 (转)

    点击下载 EcanConvertToCh.rar 主要功能就是将汉字转换成全拼的拼音,只要你输入一个汉字就会相应的转成拼音 /// <summary> /// 编 码 人:苏飞 /// 联 ...

  7. [JS] JavascriptHelp (转载)

    using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Secu ...

  8. C# DbHelperSQLP,操作不同的数据库帮助类 (转载)

    本类主要是用来访问不同数据库而编写的主要功能如下 .数据访问基础类(基于不同数据库),主要是用来访问不同数据库的. .得到最大值:是否存在:是否存在: . 执行SQL和Orace语句,返回影响的记录数 ...

  9. python基础知识八

    当你的程序中出现某些 异常的 状况的时候,异常就发生了.例如,当你想要读某个文件的时候,而那个文件不存在.或者在程序运行的时候,你不小心把它删除了.上述这些情况可以使用异常来处理. 如你的程序中有一些 ...

  10. nodejs框架express4.x 学习--安装篇

    一.安装建立项目 1.安装nodejs 2.安装express(全局) npm install -g express 默认安装的是4.12.4 3.由于在3.6版本之后项目构建器被单独拆分出来,所以还 ...