LeetCode::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.
这道题目是前一个题目的进化版,因为反复了的那个数字会被所有删去,这里面临一个多引入一个指针,来表征须要删除的节点的前一个节点,再多引入一个布尔值,
来标记究竟这个数字是不是反复了;假设反复,位于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 [具体分析]的更多相关文章
- 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] Remove Duplicates from Sorted List II 移除有序链表中的重复项之二
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [Leetcode] Remove Duplicates From Sorted Array II (C++)
题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...
- [Leetcode] Remove duplicates from sorted array ii 从已排序的数组中删除重复元素
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [LeetCode] Remove Duplicates from Sorted Array II [27]
题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- [leetcode]Remove Duplicates from Sorted Array II @ Python
原题地址:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ 题意: Follow up for &quo ...
- [LeetCode] Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- [leetcode]Remove Duplicates from Sorted List II @ Python
原题地址:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-list-ii/ 题意: Given a sorted link ...
随机推荐
- HTML中属性ID和属性NAME的区别(转)
ID和Name都可以用来标识一个标记,Javascript分别有两个方法getElementById和getElementByName来定位Dom节点. 区别如下: 1.我们知道在网页做Post提交时 ...
- (转)jQuery Validate 表单验证
在做网页表单时时常需要在客户端对表单填写的数据进行验证一番才能提交,我们可以通过自己编写JavasScript代码来验证,但是有时数据量过多时就会有些难度了.基于jQuery的jquery.valid ...
- ORA-16014: 日志 1 的序列号 242 未归档, 没有可用的目的地
SQL> alter database open; *第 1 行出现错误:ORA-16014: 日志 1 的序列号 242 未归档, 没有可用的目的地ORA-00312: 联机日志 1 线程 1 ...
- 前台任意页面调用自定义字段选项 box 单选 多选方法及查询
在模板页增加函数,函数写法比较特殊,但是v9模板引擎nb,能够识别 <? function xbox($field,$na){ $a=p ...
- Web 前端开发者必知CSS 属性
1. 圆角效果 如今的Web设计在不断跟进最新的开发技术,纷纷采用HTML5来开发多样性的Web应用.HTML5的优势之一,就是之前必须用图片实现的元素,现在可以用代码来实现. “border-ra ...
- Python文件处理之文件读取方式(二)
Python的open文件的读取方式有以下几种方法: read([size]):读取文件,如果传了size参数,则读取size字节,否则读取全部 readline([size]):读取一行 readl ...
- 配置一个servlet程序
<!-- 配置一个servlet程序 --> <servlet> <!-- servlet的内部名称 ,可以自定义--> <servlet-name>H ...
- IMAGE_SECTION_HEADER
typedef struct _IMAGE_SECTION_HEADER { BYTE Name[IMAGE_SIZEOF_SHORT_NAME]; union { DWORD PhysicalAdd ...
- TagHelper
TagHelper是怎么实现的 众所周知,在asp.net core中编写Razor视图的时候,用了一种新的写法--TagHelper 那这个TagHelper是怎么回事呢? 首先来看看TagHe ...
- web安全:sql 注入
sql注入获取webshell寻找sql注入页面,操作数据库的地方向网站写入sql语句' union select 1,2, '<?php system($_GET["cmd" ...