Remove Duplicates from Sorted ListII
给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 没有重复出现 的数字。
示例 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的更多相关文章
- [LeetCode] Remove Duplicates from Sorted List 移除有序链表中的重复项
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- [LeetCode] Remove Duplicates from Sorted List II 移除有序链表中的重复项之二
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- Leetcode-83 Remove Duplicates from Sorted List
#83. Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such that ...
- Remove Duplicates from Sorted List II
Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...
- Remove Duplicates From Sorted Array
Remove Duplicates from Sorted Array LeetCode OJ Given a sorted array, remove the duplicates in place ...
- 【leetcode】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- 26. Remove Duplicates from Sorted Array
题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...
随机推荐
- 6-12mysql库的操作
1,mysql库的各种分类: nformation_schema: 虚拟库,不占用磁盘空间,存储的是数据库启动后的一些参数,如用户表信息.列信息.权限信息.字符信息等. performance_sch ...
- Webstorm快捷操作
设置和使用技巧:前端工具开发利器webstrom专篇...更新中 选中行上下移:cl+shift+上下箭头 展示文件结构图:view-tool_window-structure.具体的图标含义 生成注 ...
- Shiro入门 - 通过自定义Realm连数数据库进行认证(md5+salt形式)
shiro-realm-md5.ini [main] #定义凭证匹配器 credentialsMatcher=org.apache.shiro.authc.credential.HashedCrede ...
- IOC 和DI的区别
什么是spring的IOC AOP? - Goluck98的专栏 - 博客频道 - CSDN.NET---看前面的那段http://blog.csdn.net/goluck98/article/det ...
- Java类加载双亲委托模式优点
启动类加载器可以抢在标准扩展类加载器之前去装载类,而标准扩展类装载器可以抢在类路径加载器之前去加载那个类,类路径装载器又可以抢在自定义类装载器之前去加载类.所以Java虚拟机先从最可信的Java核心A ...
- IntelliJ IDEA 导入eclipse项目包及附属包
使用IntelliJ IDEA 工具导入eclipse项目包,并添加另外一个项目包为库文件 1.导入项目包1,如Demo1,File-->New--->Progect From Exist ...
- qtcreator 快捷键常用
F1 查看帮助F2 跳转到函数定义(和Ctrl+鼠标左键一样的效果)Shift+F2 声明和定义之间切换F4 头文件和源文件之间切换Ctrl+1 ...
- shell-自动按省市建立文件夹,并在每个城市下创建当前日期文件夹
Code: #!/bin/bash date=`date +%Y%m%d` ProName="广东市" CityName="广州市 汕尾市 阳江市 揭阳市 茂名市 江门市 ...
- php链式调用(链式操作)
2017年6月28日 10:41:19 星期三 情景: 在多次处理数组的时候, 要自定义好多个临时变量, 起名字特别麻烦 于是, 就想到利用PHP的 1.魔法方法__call 2.不定参数, 参数自动 ...
- mybatis:在springboot中的配置
## Mybatis 配置 mybatis.type-aliases-package=com.xfind.core.entity.xianyu mybatis.mapper-locations=cla ...