【Leetcode】【Medium】Remove Duplicates 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,
Given 1->2->3->3->4->4->5, return 1->2->5.
Given 1->1->1->2->3, return 2->3.
解题思路1:
拿到题先思考,别急着写代码。什么样的情况能判定此node不是重复的:
1、node在list第一个,并且和后一个结点值不相等;
2、连续三个结点值都不相等,那么中间那个node就可判定为非重复的;
3、最后一个node如果和倒数第二个结点的值不相等,那么此node也是有效结点。
有了上面的分析,模拟思维的过程,就能得到code1:
(注意新链表添加完成后,要将最后一个结点指向空。)
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *deleteDuplicates(ListNode *head) {
if (!head || !head->next)
return head;
ListNode *newhead = new ListNode();
ListNode *newlist = newhead;
ListNode* ctrl = head; if (ctrl->val != ctrl->next->val) {
newlist->next = ctrl;
newlist = newlist->next;
} while (ctrl->next) {
if ((ctrl->val != ctrl->next->val) &&
(!ctrl->next->next || ctrl->next->val != ctrl->next->next->val)) {
newlist->next = ctrl->next;
newlist = newlist->next;
}
ctrl = ctrl->next;
} newlist->next = NULL;
return newhead->next;
}
};
解题思路2:
上面的思路相当于每次移动一个结点作为目标结点,然后判断其与左右两边结点的值是否相同;
每次移动一位,判断2次。如果遇到连续多个重复结点,那么效率就会低。
因此,遍历链表结点:
1、先记录第一个结点node_o;
2、找到前后值不一样的结点node1、node2;
3、如果node_o和node1不相同,说明node1不存在重复,那么node1是有效结点;
如果不同,说明node1和前面结点重复,那么重新将node2作为起始结点,继续重复1步骤。
代码:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *deleteDuplicates(ListNode *head) {
if (!head || !head->next)
return head;
ListNode *newhead = new ListNode();
ListNode *newlist = newhead;
newlist->next = head;
ListNode* ctrl = head; while (ctrl) {
while (ctrl->next && ctrl->val == ctrl->next->val)
ctrl = ctrl->next; if (newlist->next == ctrl)
newlist = newlist->next;
else
newlist->next = ctrl->next;
ctrl = ctrl->next;
} return newhead->next;
}
};
【Leetcode】【Medium】Remove Duplicates from Sorted List II的更多相关文章
- 82. Remove Duplicates from Sorted List II【Medium】
82. Remove Duplicates from Sorted List II[Medium] Given a sorted linked list, delete all nodes that ...
- 【leetcode】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- 【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】80. Remove Duplicates from Sorted Array II (2 solutions)
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- [leetcode] 80. Remove Duplicates from Sorted Array II (Medium)
排序数组去重题,保留重复两个次数以内的元素,不申请新的空间. 解法一: 因为已经排好序,所以出现重复的话只能是连续着,所以利用个变量存储出现次数,借此判断. Runtime: 20 ms, faste ...
- LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
- LeetCode 82. 删除排序链表中的重复元素 II(Remove Duplicates from Sorted List II)
82. 删除排序链表中的重复元素 II 82. Remove Duplicates from Sorted List II 题目描述 给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中没有 ...
- LeetCode之“链表”:Remove Duplicates from Sorted List && Remove Duplicates from Sorted List II
1. Remove Duplicates from Sorted List 题目链接 题目要求: Given a sorted linked list, delete all duplicates s ...
- leetcode 203. Remove Linked List Elements 、83. Remove Duplicates from Sorted List 、82. Remove Duplicates from Sorted List II(剑指offer57 删除链表中重复的结点)
203题是在链表中删除一个固定的值,83题是在链表中删除重复的数值,但要保留一个:82也是删除重复的数值,但重复的都删除,不保留. 比如[1.2.2.3],83题要求的结果是[1.2.3],82题要求 ...
- Leetcode: Remove Duplicates from Sorted List II 解题报告
Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...
随机推荐
- android 报错: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v4/animation/AnimatorCompatHelper;
在使用SmartRefreshLayout时,报 java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v4/a ...
- Android中直接打开高德APP进行导航
1.判断是否安装有高德APP //高德APPprivate PackageManager mPackageManager;private static List<String> mPack ...
- (转)Linux-HA实战(1)— Heartbeat安装
原文:http://blog.csdn.net/liaomin416100569/article/details/76087448-------centos7源代码编译安装heartbeat 原文:h ...
- LumiSoft
SVN: https://svn.lumisoft.ee:8443/svn/LumiSoft_Net/ User: readonly Password: readonly Download: http ...
- 比较2个文件内容不同行的shell脚本
第一种:grep命令法 命令如下:grep -vxFf file1 file2 > a.txt 其中file2是大文件,file1是小文件 第一种:comm命令法 命令如下:comm file ...
- SQL语句表处理
1.create table:创建表,语法: create table 表格名 ( 列名 数据类型, 列名 数据类型, ...... ) 2.constrain:约束,关键词 NOT NULL. UN ...
- java--集合框架总结1--set总结
一.集合框架的概述. 基础的数据结构有数组,链表,栈,队列,二叉树等,java中的数据结构,利用了这些基本的数据结构分别实现了很丰富的集合框架类型,下面简单地总结下关于java集合框架的基础内容,在进 ...
- MySQL中文问题
-- 设置客户端显示字符集 mysql>set names utf8; -- 建表时设置表的字符集和引擎 CREATE TABLE table ( `abc` char(32) NOT NULL ...
- 多个tomcat配置
在centos7.3下搭建jenkins自动部署环境,需要一个tomcat来启动jenkins,另一个用来自动部署的位置,因此需要两个tomcat同时运行,并且在自动构建后能够启动项目,又不会关闭je ...
- java利用直方图实现图片对比
需求 实现两张图对比,找出其中不同的部分. 分析 首先将大图切片,分成许多小图片.然后进行逐个对比,并设定相似度阈值,判断是否是相同.最后整理,根据生成数组标记不同部分.如果切片足够小,便越能精确找出 ...