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

Example 1:

Input: 1->2->3->3->4->4->5
Output: 1->2->5

Example 2:

Input: 1->1->1->2->3
Output: 2->3

和之前那道 Remove Duplicates from Sorted List 不同的地方是这里要删掉所有的重复项,由于链表开头可能会有重复项,被删掉的话头指针会改变,而最终却还需要返回链表的头指针。所以需要定义一个新的节点,然后链上原链表,然后定义一个前驱指针和一个现指针,每当前驱指针指向新建的节点,现指针从下一个位置开始往下遍历,遇到相同的则继续往下,直到遇到不同项时,把前驱指针的next指向下面那个不同的元素。如果现指针遍历的第一个元素就不相同,则把前驱指针向下移一位。代码如下:

解法一:

class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if (!head || !head->next) return head;
ListNode *dummy = new ListNode(-), *pre = dummy;
dummy->next = head;
while (pre->next) {
ListNode *cur = pre->next;
while (cur->next && cur->next->val == cur->val) {
cur = cur->next;
}
if (cur != pre->next) pre->next = cur->next;
else pre = pre->next;
}
return dummy->next;
}
};

同样,我们也可以使用递归来做,首先判空,如果 head 为空,直接返回。然后判断,若 head 之后的结点存在,且值相等,那么先进行一个 while 循环,跳过后面所有值相等的结点,到最后一个值相等的点停下。比如对于例子2来说,head 停在第三个结点1处,然后对后面一个结点调用递归函数,即结点2,这样做的好处是,返回的值就完全把所有的结点1都删掉了。若 head 之后的结点值不同,那么还是对 head 之后的结点调用递归函数,将返回值连到 head 的后面,这样 head 结点还是保留下来了,因为值不同嘛,最后返回 head 即可,参见代码如下:

解法二:

class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if (!head) return head;
if (head->next && head->val == head->next->val) {
while (head->next && head->val == head->next->val) {
head = head->next;
}
return deleteDuplicates(head->next);
}
head->next = deleteDuplicates(head->next);
return head;
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/82

类似题目:

Remove Duplicates from Sorted List

参考资料:

https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/

https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/discuss/28335/My-accepted-Java-code

https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/discuss/28339/My-Recursive-Java-Solution

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Remove Duplicates from Sorted List II 移除有序链表中的重复项之二的更多相关文章

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

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

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

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

  3. 82. Remove Duplicates from Sorted List II(删除有序链表中的重复元素)

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

  4. 力扣 ——Remove Duplicates from Sorted List II(删除排序链表中的重复元素 II)python实现

    题目描述: 中文: 给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 没有重复出现 的数字. 示例 1: 输入: 1->2->3->3->4->4-> ...

  5. 【LeetCode每天一题】Remove Duplicates from Sorted Array II(移除有序数组中重复的两次以上的数字)

    Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...

  6. LeetCode Remove Duplicates from Sorted Array II 删除整型数组中的重复元素并返回剩下元素个数2

    class Solution { public: int removeDuplicates(int A[], int n) { ],*e=&A[]; //s指向“连续数字”的第一个,e往后遍历 ...

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

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

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

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

  9. [LeetCode] 83. Remove Duplicates from Sorted List 移除有序链表中的重复项

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

随机推荐

  1. Android重构与设计之路,从整理提示弹窗(SmartAlertPop)开始

    封装一个独立弹窗Module,这里的弹窗包括普通的Dialog方式弹框和WindowManager方式弹窗.提供一种管理项目里面弹窗的方案,便于后期修改和维护. 首先描述一个在大项目中普遍存在的一个现 ...

  2. 报错:已有打开的与此命令相关联的 DataReader,必须首先将它关闭。

    SqlParameter[] sp = { new SqlParameter("@nGridID",SqlDbType.BigInt), new SqlParameter(&quo ...

  3. 数据仓库开发——Kettle使用示例

    Kettle是一个开园ETL工具,做数据仓库用Spoon. 工具:下载Spoon,解压即可用   1.认识常用组件:     表输入     插入\更新     数据同步     文本文件输出     ...

  4. HTML5学习总结——canvas绘制象棋(canvas绘图)

    一.HTML5学习总结——canvas绘制象棋 1.第一次:canvas绘制象棋(笨方法)示例代码: <!DOCTYPE html> <html> <head> & ...

  5. HTTPS和HTTP的区别

    (转自:http://www.php100.com/html/it/biancheng/2015/0209/8582.html) 总的来说,http效率更高,https安全性更高. 首先谈谈什么是HT ...

  6. Shiro安全框架入门篇(登录验证实例详解与源码)

    转载自http://blog.csdn.net/u013142781 一.Shiro框架简单介绍 Apache Shiro是Java的一个安全框架,旨在简化身份验证和授权.Shiro在JavaSE和J ...

  7. Apache Spark 1.6 Hadoop 2.6 Mac下单机安装配置

    一. 下载资料 1. JDK 1.6 + 2. Scala 2.10.4 3. Hadoop 2.6.4 4. Spark 1.6 二.预先安装 1. 安装JDK 2. 安装Scala 2.10.4 ...

  8. iOS从零开始学习直播之音频4.歌词

      上一篇讲了歌曲的切换,这一篇主要讲歌词部分的实现.   先看效果图.当歌手唱到这句歌词时候,我们要标记出来,这里显示字体为黄色. 1.获取歌词   一般歌词都是一个链接.类似于"http ...

  9. iOS从零开始学习直播之音频2.后台播放和在线播放

    本篇主要讲音频的后台播放和在线播放. 后台播放   上一篇写的工程运行之后程序退至后台,发现运行不了,歌停止了,这显然不行,音乐后台播放是标配啊.今天就来讲一下后台播放. 1.在plist文件里,告诉 ...

  10. Looper.prepare()和Looper.loop()

    什么时候需要 Looper Looper用于封装了android线程中的消息循环,默认情况下一个线程是不存在消息循环(message loop)的,需要调用Looper.prepare()来给线程创建 ...