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的更多相关文章

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

  2. 【leetcode】Remove Duplicates from Sorted Array II

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

  3. 【LeetCode练习题】Remove Duplicates from Sorted List II

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

  4. 【LeetCode】80. Remove Duplicates from Sorted Array II (2 solutions)

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

  5. [leetcode] 80. Remove Duplicates from Sorted Array II (Medium)

    排序数组去重题,保留重复两个次数以内的元素,不申请新的空间. 解法一: 因为已经排好序,所以出现重复的话只能是连续着,所以利用个变量存储出现次数,借此判断. Runtime: 20 ms, faste ...

  6. LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>

    LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...

  7. LeetCode 82. 删除排序链表中的重复元素 II(Remove Duplicates from Sorted List II)

    82. 删除排序链表中的重复元素 II 82. Remove Duplicates from Sorted List II 题目描述 给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中没有 ...

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

  9. 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题要求 ...

  10. Leetcode: Remove Duplicates from Sorted List II 解题报告

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

随机推荐

  1. 【爬虫】-xpath语法熟悉及实战

    本文为自学记录,部分内容转载于 w3school python3网络爬虫实战 知乎专栏:写点python 如有侵权,请联系删除. 语法 1.选取节点 XPath 使用路径表达式在 XML 文档中选取节 ...

  2. PHP设置时区

    <?php//设置默认的时区date_default_timezone_set('Asia/Shanghai');//输出1396193923对应的日期echo date("Y-m-d ...

  3. MySQL存储过程中判断形参是否为空null

    直接看例子: DELIMITER $$CREATE DEFINER=`root`@`127.0.0.1` PROCEDURE `restore`(username varchar(50))BEGINi ...

  4. 九度oj题目1555:重复子串

    题目1555:重复子串 时间限制:3 秒 内存限制:256 兆 特殊判题:否 提交:738 解决:125 题目描述: 给定一个由小写字母组成的字符串,求它的所有连续子串中,出现过至少两次,且至少有一对 ...

  5. Hadoop科普文—常见的45个问题解答

    1.Hadoop集群可以运行的3个模式? 单机(本地)模式 伪分布式模式 全分布式模式 2.  单机(本地)模式中的注意点? 在单机模式(standalone)中不会存在守护进程,所有东西都运行在一个 ...

  6. 最小化安装的redhat/centos安装gnome桌面

    因系统版本和语言环境不同,安装包的名字会有所差别 安装方式可以选择直接联网安装,也可以加载dvd镜像源安装,这里针对配置redhat/centos本地dvd的yum源做个记录: 1.复制 redhat ...

  7. Firebird 条件函数

    1.iif  IIF (<condition>, ResultT, ResultF) 示例: select iif( sex = 'M', 'Sir', 'Madam' ) from Cu ...

  8. MySQL函数库

    MySQL函数库,这个函数库是一个外部函数库!这个函数提供了对于MySQL数据库进行操作的常用函数,如连接MySQL服务器.打开数据库.执行SQL语句等.所以这个函数库的功能对于我们来说是非常重要的! ...

  9. 暗示net core

    using (var scope = ServiceProvider.CreateScope()){ var aSubscriber = Activator.CreateInstance(aSubsc ...

  10. mybatis学习之入门实例

    测试版本 mybatis:3.2.8 数据库:mysql 项目结构 jar包准备 mybatis-3.2.8.jar mysql-connector-java-5.1.39-bin.jar junit ...