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. (转)基于keepalived搭建MySQL的高可用集群

    基于keepalived搭建MySQL的高可用集群  原文:http://www.cnblogs.com/ivictor/p/5522383.html MySQL的高可用方案一般有如下几种: keep ...

  2. MySQL prompt提示符总结

      A counter that increments for each statement you issue \D 当前日期 \d 当前数据库 \h 数据库主机 \l The current de ...

  3. HTTP传输内容的压缩

    最近在看尤大的ssr项目的demo,看他的项目里有用到compression,完全看不懂这是什么鬼,然后百度了一下,文档也都是英文的,看着有点吃力,隐约的觉得这是压缩http请求的,做前端的都知道,在 ...

  4. java服务器访问其他服务器工具类编写

    java服务器访问其他服务器工具类编写适合各种消息推送及微服务交互 package com.xiruo.medbid.components; import com.xiruo.medbid.util. ...

  5. android studio应用获取系统属性权限(SystemProperties)

    dependencies { provided files(getLayoutLibPath()) } /** ZhangChao time:2014-12-31,get layoutlib.jar ...

  6. C# 多线程系列之Mutex使用

    互斥量是一个内核对象,它用来确保一个线程独占一个资源的访问,并且互斥量可以用于不同进程中的线程互斥访问资源. 我们可以把Mutex看作一个出租车,乘客看作线程.乘客首先等车,然后上车,最后下车.当一个 ...

  7. 使用Electron开发桌面应用

    Electron 框架的前身是 Atom Shell,可以让你写使用 JavaScript,HTML 和 CSS 构建跨平台的桌面应用程序.它是基于io.js 和 Chromium 开源项目,并用于在 ...

  8. number_formate 货币金额或数量格式化

    $row['formated_goods_price']    = number_format($row['goods_price'], 2, '.', ''); number_format() 函数 ...

  9. Eclipse提示workspaces is use

    问题描述: 有时候因为强行关闭Eclipse导致再次打开出现workspace提示正在使用 解决办法: 删除workspace目录下隐藏文件夹 .metadata 中的 .lock 文件 worksp ...

  10. Spring_Spring与IoC_第一个程序

    一.IoC IoC是一种概念,是一种思想,指将传统上由程序代码直接操控的对象调用权交给容器,通过容器来实现对象的装配和管理.控制反转是对对象控制权的转移,从程序代码本身反转到外部容器. 当前IoC比较 ...