给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 没有重复出现 的数字。

示例 1:

输入: 1->2->3->3->4->4->5
输出: 1->2->5

示例 2:

输入: 1->1->1->2->3
输出: 2->3 思路:(1)因为头结点可能被删除,所以要定义头结点;(2)定义两个指针,一个指向前驱结点,另一个指针指向前驱节的下一个结点,并不断找到值相等的结点。
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) { if(!head || !head->next)
return head; ListNode* dummy = new ListNode(-); ListNode* p = dummy;
dummy->next = head;
while(p->next)
{
ListNode* cur = p->next; while(cur->next && cur->val == cur->next->val)
cur = cur->next; if(cur != p->next)
p->next = cur->next;
else
p = p->next; } return dummy->next;
}
};

    

Remove Duplicates from Sorted ListII的更多相关文章

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

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

  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. [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  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-83 Remove Duplicates from Sorted List

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

  6. Remove Duplicates from Sorted List II

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

  7. Remove Duplicates From Sorted Array

    Remove Duplicates from Sorted Array LeetCode OJ Given a sorted array, remove the duplicates in place ...

  8. 【leetcode】Remove Duplicates from Sorted Array II

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

  9. 26. Remove Duplicates from Sorted Array

    题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...

随机推荐

  1. Debian Security Advisory(Debian安全报告) DSA-4405-1 openjpeg2

    package :openjpeg2 相关CVE ID: CVE-2017-17480 CVE-2018-5785 CVE-2018-6616 CVE-2018-14423 CVE-2018-1808 ...

  2. ubuntu18.04下安装mysql后无法用mysqlworkbench访问

    问题描述:我在ubuntu18.04下执行以下命令安装mysql时遇到了mysqlworkbench无法连接root用户的问题.ubuntu18.04下默认安装mysql时是5.7版本的,但是5.7版 ...

  3. Idea快捷操作

    command+N 导入jar包 在方法体内部有for循环,在IntellJ中是输入fori,然后会有一个提示,选中需要的for循环即可 System.out.println();在IntellJ中是 ...

  4. mongodb 系列 ~ mongo的两种引擎介绍对比

    一 简介 两种引擎方式的对比二  对比与说明   1 版本支持      MMAP引擎 3.2版本之前,默认引擎       WT 引擎 3.2版本之后,默认引擎   2 并发性能(核心)     M ...

  5. python - 文件系统和文件

    文件系统和文件        文件系统是os用于明确磁盘或分区上的文件的方法和数据结构--即在磁盘上组织文件的方法        计算机文件,是存储在某种长期储存设备或临时存储设备中的一段数据流,并且 ...

  6. iTOP-4418开发板Ubuntu系统烧写方法分享

    开发平台:迅为iTOP-4418开发板    系统:Ubuntu   1. TF卡读写速度测试 烧写 Ubuntu 对于 TF 卡的要求比较高,很多老旧的卡都无法烧写 Ubuntu,下面提供一种 相对 ...

  7. shell编程 之 文件包含

    解释:就是在一个脚本中引用或者运行其他脚本的文件. 常用格式:. filename 或者 source filename 实例:/hehe文件夹下有两个文件:t2.sh 和t3.sh t2.sh的内容 ...

  8. python3字典中items()和python2.x中iteritems()有什么不同?

    在Python2.x中: items() 用于返回一个字典的拷贝列表[Returns a copy of the list of all items (key/value pairs) in D],占 ...

  9. Python3-操作系统发展史

    操作系统发展史 手工操作 —— 穿孔卡片 批处理 —— 磁带存储 多道程序系统 操作系统的作用 手工操作 —— 穿孔卡片 1946年第一台计算机诞生--20世纪50年代中期,计算机工作还在采用手工操作 ...

  10. for..of和for..in和map、filter等循环区别

    1.for in遍历的是数组的索引(即键名),而for of遍历的是数组元素值. for in遍历比较适合遍历对象,不太适合数组,有可能遍历出来的不按照顺序 遍历数组 ,,,,,] for (var ...