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.

这道题目是前一个题目的进化版,因为反复了的那个数字会被所有删去,这里面临一个多引入一个指针,来表征须要删除的节点的前一个节点,再多引入一个布尔值,

来标记究竟这个数字是不是反复了;假设反复,位于pos 以及 next_pos之间的节点必须都删除(闭区间),pre_pos 非常显然原地不动,当发现不是反复,那么pre_pos

向前走一格。

一个注意点:在思考链表类问题的时候,必须非常明白每一个指针标志着什么,而且须要再用到诸如a->next  a->val时特别确信a != NULL, 这个必须考虑到边界条件,如

何时走到了最后,或者链表仅仅有一个节点或者没有节点,出现了特殊情况等等,这个是RUNTIME ERROR的关键问题

<span style="font-family: Arial, Helvetica, sans-serif;">ListNode *deleteDuplicates(ListNode *head) {</span>
        if (head == NULL || head->next == NULL)
return head; bool isDup;
ListNode *pos = head, *next_pos = head; ListNode *pre_pos = new ListNode(0);
ListNode *new_head = pre_pos;
pre_pos->next = head; while (next_pos != NULL)
{
next_pos = next_pos->next;
isDup = false;
while (next_pos != NULL && pos->val == next_pos->val)
{
isDup = true;
next_pos = next_pos->next;
}
if (isDup)
{
pre_pos->next = next_pos;
pos = next_pos;
}
else
{
pre_pos = pre_pos->next;
pos = pos->next;
}
}
return new_head->next;
}
};

LeetCode::Remove Duplicates from Sorted List II [具体分析]的更多相关文章

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

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

  2. [LeetCode] Remove Duplicates from Sorted List II 移除有序链表中的重复项之二

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  3. [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  4. [Leetcode] Remove Duplicates From Sorted Array II (C++)

    题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...

  5. [Leetcode] Remove duplicates from sorted array ii 从已排序的数组中删除重复元素

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  6. [LeetCode] Remove Duplicates from Sorted Array II [27]

    题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...

  7. [leetcode]Remove Duplicates from Sorted Array II @ Python

    原题地址:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ 题意: Follow up for &quo ...

  8. [LeetCode] Remove Duplicates from Sorted List II

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  9. [leetcode]Remove Duplicates from Sorted List II @ Python

    原题地址:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-list-ii/ 题意: Given a sorted link ...

随机推荐

  1. mysql source命令导入sql文件效率分析和索引整理

    Query OK, 24918 rows affected (0.90 sec)Records: 24918  Duplicates: 0  Warnings: 0 Query OK, 24923 r ...

  2. jQuery三种事件绑定方式.bind(),.live(),.delegate()

    .bind(), .live(), 和 .delegate()之间的区别并不明显.但是理解它们的不同之处有助于写出更简洁的代码,并防止我们的交互程序中出现没有预料到的bug. 基础 DOM树 首先,图 ...

  3. java BigDecimal的操作

    今天给大家讲一下java中BigDecimal的操作.由于double,float的精度不够,因此在进行商业计算的时候要使用的BigDecimal.BigDecimal对象创建如下: BigDecim ...

  4. Swift - 39 - 枚举类型关联默认值

    //: Playground - noun: a place where people can play import UIKit enum Month: Int { // 这么定义, 后面的Feb, ...

  5. "float: left;" div 不换行显示

    <div id='p'> <div id='c1'> </div> <div id='c2'> </div> <div id='c3' ...

  6. Android Fragment用法之给Activity创建事件回调

    在某些案例中,可能需要Fragment与Activity共享事件.在Fragment内部定义一个回调接口是一个好方法,并且规定由持有它的Activity实现这个回调方法.当Activity通过接口接受 ...

  7. 【C#编程基础学习笔记】4---Convert类型转换

    2013/7/24 技术qq交流群:JavaDream:251572072  教程下载,在线交流:创梦IT社区:www.credream.com [C#编程基础学习笔记]4---Convert类型转换 ...

  8. ACM中常用的C/C++函数

    只大概说明功能,具体用法请自行百度. C函数 memset:按字节填充地址空间 sscanf:从一个字符串中格式化读取变量 sprintf:将变量格式化写入字符串中 atoi:字符串转int atof ...

  9. PHPCMS V9二次开发]自定义字段模型-文本组

    phpcms v9,我们在做类似于酒店房型等类型的时候,需要用到文本组字段模型,但phpcms并未提供该模型.如下图所示效果: 展示效果如下: 步骤/方法 打开phpcms\modules\conte ...

  10. centos6.2下安装星际译王stardict3.0

    星际译王是一个Linux下很好的翻译软件. 我的系统是centos6.2 32位版.本来在http://code.google.com/p/stardict-3/downloads/list 上下的源 ...