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 ...
随机推荐
- 让页脚footer永远固定在页面的底部,而不是永远固定在显示器屏幕的底部的方法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- spring源码分析
编译问题 spring-4.0.5.release编译是用jdk8编译的,为啥可以运行在jdk7的环境? 源码分析 spring源码分析,由一个点各个击破,比如依赖注入,autowired. spri ...
- ASP.NET协作应用集成到trsids身份验证服务器的开发流程
开发Actor协同模块: (参考TRSIDS4.0 协作应用集成手册[asp.net]) ASP.Net协作应用集成到IDS之前,需要开发Actor类实现协作应用回调接口中定义的本地登录.退出.用户信 ...
- 分享内容到微博、QQ空间、人人网、开心网等社区
网上有不少分享内容到微博.QQ空间.人人网.开心网等社区的插件,但它们都有自己固定的样式,你不一定会喜欢. 或许你想保持你的网站的原状,添加上微博.QQ空间.人人网.开心网的LOGO图片,点击之后就可 ...
- SQL Server 2008将数据导出为脚本
之前我们要将一个表中的数据导出为脚本,那么只有在网上找一个导出数据的Script,然后运行就可以导出数据脚本了.现在在SQL Server 2008的Management Studio中增加了一个新特 ...
- jsp连接MySQL操作GIS地图数据,实现添加point的功能
index_map.jsp中的代码: <%@ page language="java" pageEncoding="utf-8"%> <%@ ...
- RBAC角色权限设计思路
1 设计思路 为了设计一套具有较强可扩展性的用户认证管理,需要建立用户.角色和权限等数据库表,并且建立之间的关系,具体实现如下. 1.1 用户 用户仅仅是纯粹的用户,用来记录用户相关信息,如用户名.密 ...
- flask开发restful api系列(6)-配置文件
任何一个好的程序,配置文件必不可少,而且非常重要.配置文件里存储了连接数据库,redis的用户密码,不允许有任何闪失.要有灵活性,用户可以自己配置:生产环境和开发环境要分开,最好能简单的修改一个东西, ...
- Date对象
<script type="text/javascript"> /* 日期对象(Date) */ var date = new Date(); //获取到当前的系统时间 ...
- IRP派遣操作
IRPTrace工具跟踪IRP 派遣函数(Dispathc Funtion)是windows驱动中的重要概念.驱动程序的主要功能是负责处理I/O请求,其中大部分I/O请求是在派遣函数中处理的.用户模式 ...