[Leetcode] Remove duplicate from sorted list ii 从已排序的链表中删除重复结点
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
For example,
Given1->2->3->3->4->4->5, return1->2->5.
Given1->1->1->2->3, return2->3.
这题和Remove duplicate from sorted list的区别在于,本题中,只要结点只要出现重复则该值相等的结点都要删除,上题中留一个不删。
思路:这里有可能有修改表头(如:1->1->1>2>3),一般修改表头的题目都会需要一个辅助指针,所以要新建一个结点。遍历链表,遇到相等的相邻结点,直接继续遍历;遇到不相等的两相邻结点时,若pre->next=cur说明cur没有重复的,pre=pre->next即可,若是不等于说明,可能有重复,则,pre连接cur但是pre不移动,需重新进入循环检验是否没有重复(没有重复时,pre->next=cur),直到没有重复结点。源代码
主要的思想是:保持前指针pre不动,因为pre没有重复,所以用其next去和后面的比较。当遇到值不相等时,判断中间是否有重复,即pre->next=cur(这里判断cur是否为pre的后缀,不是值的判断),没有,pre移动,有,则pre不动,但和后面的连接起来,继续重复。
class Solution {
public:
ListNode *deleteDuplicates(ListNode *head)
{
if(head==NULL) return head;
ListNode *newList=new ListNode(-);
newList->next=head;
ListNode *pre=newList;
ListNode *cur=head;
while(cur)
{
while(cur->next&&pre->next->val==cur->next->val)
{
cur=cur->next;
}
if(pre->next==cur) //想明白!不是值。
pre=pre->next;
else
{
pre->next=cur->next;
}
cur=cur->next;
}
return newList->next;
}
};
[Leetcode] Remove duplicate from sorted list ii 从已排序的链表中删除重复结点的更多相关文章
- [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 list 从已排序的链表中删除重复元素
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- leetcode 题解:Remove Duplicates from Sorted Array II(已排序数组去三次及以上重复元素)
题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...
- [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: Remove Duplicates from Sorted List II 解题报告
Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...
- [LeetCode] 83. Remove Duplicates from Sorted List ☆(从有序链表中删除重复项)
描述 Given a sorted linked list, delete all duplicates such that each element appear only once. Exampl ...
- LeetCode 82,考察你的基本功,在有序链表中删除重复元素II
本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是LeetCode专题的第51篇文章,我们来看LeetCode第82题,删除有序链表中的重复元素II(Remove Duplicates ...
- [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 ...
随机推荐
- vue渲染自定义json数据
<template> <div class="wrap"> <div class="main"> <div class ...
- PHP如何实现99乘法表?
看到这个问题,可能大家更多的是考虑到用for循环,个人觉得使用for循环太影响程序性能.推荐使用递归处理. /** * Title : 递归实现99乘法表 * Author : Bruceqi * ...
- php 删除富文本编辑器保存内容中的其他代码(保留中文)
$str = '<p><p style="ve:"">测试筛选文本域内的中文 </p><p sty;"> ...
- 接口API封装中常见的HTTP状态码
在进行后端接口API封装的过程中,需要考虑各种错误信息的输出.一般情况下,根据相应问题输出适合的HTTP状态码,可以方便前端快速定位错误,减少沟通成本. HTTP状态码有很多,每个都有对应的含义,下面 ...
- doT.js使用介绍
doT.js特点是快,小,无依赖其他插件,压缩版仅有4K大小. doT.js详细使用介绍 使用方法: 1 2 3 4 5 6 7 {{ }} 模板 开始标记 结束标记 {{= }} 赋值 {{~ ...
- 虚拟机服务没有启动的 CentOS 和 Ubuntu 无法上网
测试用 vmware 安装 OSX,安装补丁时要停止 vmware 的服务.如下图: 结果忘记启动了,导致 centos\ubuntu 等所有虚拟机都无法上网...所有的 启动这四个服务后,一切恢复正 ...
- Python全栈day 02
Python全栈day 02 一.循环语句 while 用法 num = 1 while num <= 10: print(num) num += 1 # 循环打印输出1-10 while el ...
- node解析post表单信息
一共有4种解析方式 urlencoded.json.text .raw 发起请求的form表单中可以设置三种数据编码方式 application/x-www-form-urlencoded.multi ...
- 洛谷P1090 合并果子
合并果子 题目链接 这个只能用于结构体中 struct item { int val; friend bool operator < (item a,item b) { return a.val ...
- 手把手教你写css3通用动画
之前接了几个微信里的项目,类似电子邀请函,什么分析报告这样的项目, 对css3动画要求十分高,每个页面客户几乎都有天马行空的想法,或者说设计师有这样的想法.众所周知css3里的keyframe写好了就 ...