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. (转)linux exec与重定向

    原文:http://xstarcd.github.io/wiki/shell/exec_redirect.html linux exec与重定向 exec和source都属于bash内部命令(buil ...

  2. debian sudo

    apt-get install sudo vi /etc/sudoers add CentOS 7 root ALL=(ALL) ALL Debian root ALL=(ALL:ALL) ALL 按 ...

  3. 转 shell中$(( )) 与 $( ) 还有${ }的区别

    原文 http://blog.zol.com.cn/2322/article_2321763.html $( ) 与 ` ` (反引号)在 bash shell 中,$( ) 与 ` ` (反引号) ...

  4. 【ExtJS】contentEl的使用

    contentEl 指定一个已存在的HTML元素, 或者一个已存在HTML元素的 id , 它们将被用作当前组件的内容. 此配置选项被用来将一个已存在的HTML元素 插入到一个新组件(在组件渲染之后它 ...

  5. word-wrap和word-break的区别吗?

    word-wrap: css的 word-wrap 属性用来标明是否允许浏览器在单词内进行断句,这是为了防止当一个字符串太长而找不到它的自然断句点时产生溢出现象. word-break: css的 w ...

  6. Win中同时安装python2和python3及SulimeText3的python IDE搭建

    一.下载安装Sublime Text3,初衷是不想忍受pycharm的打开速度,想享受下飞的质感.Sublime Text3的安装已经久远,请自行google. 二.安装python2.7与pytho ...

  7. linux 查看端口,开启新端口

    一.查看端口被占用命令 1.lsof -i:端口号 2.netstat -tunlp|grep 端口号 3.netstat -anp 查看哪些端口被打开 上面命令是查看端口被进程占用的情况 二.开启新 ...

  8. Vim常用插件——前端开发工具系列

    作为一名开发者,应该对编辑器之神Vim与神之编辑器Emacs有所耳闻吧.编辑器之战的具体细节有兴趣的童鞋可以google之. Vim最大的特点是打开速度快,功能强大,一旦掌握了其中的命令,编程过程双手 ...

  9. android studio应用修改到android源码中作为内置应用

    1. 方法一:导入,编译(太麻烦,各种不兼容问题) android studio和eclipse的应用结构目录是不同的,但是在android源码中的应用基本上都是使用的eclipse目录结构(在/pa ...

  10. php实现对数组进行编码转换

    1.转换GB2312编码为UTF-8 //更改编码为utf8 protected function array2utf8($array){ $array = array_map(function($v ...