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 ...
随机推荐
- (转)select 1 from ... sql语句中的1代表什么意思? .
select 1 from ..., sql语句中的1代表什么意思?查出来是个什么结果? select 1 from table;与select anycol(目的表集合中的任意一行 ...
- drop表,然后创建表,插入数据,并创建索引等内容。
execute immediate 'drop table sjb_jhgl_ydjhtdsbb'; execute immediate 'create table dw_sc.sjb_jhgl_ ...
- egrep和grep有什么区别
grep默认不支持正则表达式,egrep默认支持正则表达式,egrep 等于 grep -E 命令.
- ORA-16018: cannot use LOG_ARCHIVE_DEST with LOG_ARCHIVE_DEST_n or DB_RECOVERY_FILE_DEST【error收集】
之前一直没有注意一个事情, 关于设置archive归档路径设置的问题. 设置数据库为归档模式的命令: 1.首先要切换到mount状态: 2.执行alter system archivelog; 3.查 ...
- jvm参数设置
-Xss: 栈大小 -Xms:堆初始化大小-Xmx:堆最大大小-XX:NewSize=n:设置伊甸区大小-XX:NewRatio=n:年轻代与年老代比值.如:为3,表示年轻代与年老代比值是1:3, ...
- Oracle数据库之PL/SQL程序设计简介
PL/SQL程序设计简介 一.什么是PL/SQL? PL/SQL是 Procedure Language & Structured Query Language 的缩写. ORACLE的SQL ...
- IE的有条件注释详解
IE的有条件注释是一种专有的(因此是非标准的).对常规(X)HTML注释的Miscrosoft扩展.顾名思义,有条件注释使你能够根据条件(比如浏览器版本)显示代码块.尽管是非标准的,但是有条件注释对于 ...
- HTML5数据存储
介绍两种对象使用方法: sessionStorage方法如果关闭了浏览器,这个保存的数据就丢失. 1.sessionStorage 保存数据:sessionStorage.setItem(key,va ...
- await使用中的阻塞和并发
本文讨论,通过将Lambda还原成最普通的代码段,来解释上篇提出的疑问.并更正上篇中一些不太正确的写法.最后会给出无需等待Async方法返回值时,对Async方法使用await的建议,供大家参考.第一 ...
- NVIDIA 显卡温度提示过高,显卡温度高99度怎么办?
你可能没有在意您的NVIDIA(英伟达)显卡温度,那么你就不会知道显卡温度过高如何导致的呢?也不会知道如果去解决显卡温度过高的问题了,在此我希望在电脑玩游戏或者在办公中电脑速度快慢,或者发热的情况下, ...