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] 82. Remove Duplicates from Sorted List II 移除有序链表中的重复项之二的更多相关文章

  1. [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 ...

  2. [LeetCode] Remove Duplicates from Sorted List 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. LeetCode 26 Remove Duplicates from Sorted Array (移除有序数组中重复数字)

    题目链接: https://leetcode.com/problems/remove-duplicates-from-sorted-array/?tab=Description   从有序数组中移除重 ...

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

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

  6. 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 ...

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

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

  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. LeetCode 752:打开转盘锁 Open the Lock

    题目: 你有一个带有四个圆形拨轮的转盘锁.每个拨轮都有10个数字: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' .每个拨轮可以自由旋转:例如把 ' ...

  2. CSS3 滤镜Filter亮度动画

    CSS3 滤镜Filter亮度动画 -webkit-filter:brightness 值越高 亮度越亮<pre><!DOCTYPE html><html lang=&q ...

  3. logical函数

    logical函数(逻辑函数) logical(x):x ~=0时,logical(x)=1:x = 0时,logical(x)=0

  4. 02_python基础(面向对象编程)

    面向对象编程: 把一组数据结构和处理它们的方法组成对象(object),把相同行为的对象归纳为类(class),通过类的封装(encapsulation)隐藏内部细节,通过继承(inheritance ...

  5. nodejs-翻转算法

    nodejs-翻转算法 /** * Created by moon on 2019/12/14. */ //程序运行完成时一定要有输出语句,本工具才能正确展示运行结果. function abc() ...

  6. 记一次CTF出题WP

    笔者有幸参与一次CTF入门级的出题,在此记录一下WP 1.测试你得手速 1.用IDA打开程序 找到单击次数, 获取全局变量地址. 打开程序 打开OllyDbg attcach后在左下角按CTRL+N ...

  7. 点击除指定区域外的空白处,隐藏div

    <script> $(document).click(function (e) { var $target = $(e.target); //点击.zanpl和.quanzipl以外的地方 ...

  8. 以一道ctf学习python脚本

    今天做了省赛初赛的ctf比赛,过程真是忐忑,奋战了6个小时(本来是三个小时的,哈哈哈哈). 不说了! 不说了! 说多了都是泪~ 看题吧,题目就是一道流量分析题,里面有一段icmp包,icmp包的ttl ...

  9. wpf 窗体添加背景图片

    方法一:xaml中:<控件>   <控件.Background><ImageBrush ImageSource="/WpfApplication1;compon ...

  10. 我的第一次diy装机记录——小白的配置篇

    工欲善其事,必先利其器 相对于IT人来说,电脑是个好东西,应该是第二个除了手机陪伴我们最长的东西.今年4月份来的杭州,留下了那款陪我征战4年的笔记本,没有电脑,下班后的夜晚索然无味,身心的需求也日渐强 ...