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. 给EditText的drawableRight属性的图片设置点击事件 分类: 学习笔记 android 2015-07-06 13:20 134人阅读 评论(0) 收藏

    这个方法是通用的,不仅仅适用于EditText,也适用于TextView.AutoCompleteTextView等控件. Google官方API并没有给出一个直接的方法用来设置右边图片的点击事件,所 ...

  2. Android(java)学习笔记195:三重for循环的优化(Java面试题)

    1.首先我们看一段代码: for(int i=0;i<1000;i++){ for(int j=0;j<100;j++){ for(int k=0;k<10;k++){ testFu ...

  3. Android(java)学习笔记188:关于构造代码块,构造函数的一道面试题(华为面试题)

    1.源码是: package text; public class TestStaticCon { public static int a = 0; static { a = 10; System.o ...

  4. div垂直居中的方法(转)

    在说到这个问题的时候,也许有人会问CSS中不是有vertical-align属性来设置垂直居中的吗?即使是某些浏览器不支持我只需做少许的CSS Hack技术就可以啊!所以在这里我还要啰嗦两句,CSS中 ...

  5. thinkphp 常见问题

    0.写在最前面的不断更新 (1)trace不起作用 A:必须要输出到模板,才会有trace信息 (2)提示“您浏览的页面暂时发生了错误!请稍后再试-” A:检查控制器(看看能进到控制器没有,设断点输出 ...

  6. phpcms 换域名

    修改/caches/configs/system.php里面所有和域名有关的,把以前的老域名修改为新域名就可以了. 进行后台设置->站点管理   对相应的站点的域名进行修改. 更新系统缓存.点击 ...

  7. JQM 页面滚动加载

    1 应用场景:文章比较长,只加载部分,当到页面底部触发获取更多数据. 2 如图,监听滚动条的位置,触发事件,转化为求X的长度, 3 实例代码: //滚动条到底加载更多 $(document).on(& ...

  8. 规划收发你的邮件,使用qq邮箱接收阿里云企业邮邮件

    使用qq邮箱接收阿里企业邮 首先管理员开通企业邮后会发来激活短信 根据短信提示打开https://qiye.aliyun.com企业邮登陆地址 使用短信提供的密码登陆邮箱 首次登陆时会让我们重设密码 ...

  9. (转)xml序列化

    在 .NET Framework 中提供两种串行化方法,一种是二进制法,另一种是xml串行化. 序列化是将对象状态转换为可保持或传输的格式的过程,xml序列化是将对象的公共字段和属性序列化为xml流. ...

  10. 初学HTML5系列一:简单介绍

    最近很闲,就想着学点东西,然后就瞄中了html5,以前只看过很简单的一些,这次是系统的学下,顺便也记录下.废话不多说,开始正题. 稍微介绍下html5,html5是W3C和WHATWG 合作的结果. ...