Remove Duplicates from Sorted List II leetcode java
题目:
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.
题解:
这道题与I的区别就是要把所有重复的node删除。因此,还是利用I中去重的方法,引用双指针,并且增加一个新的指针,指向当前的前一个node,使用3个指针(prev,current,post)来遍历链表。
最开始还是需要建立一个fakehead,让fakehead的next指向head。然后,使用我刚才说过的3个指针方法来初始化3个指针,如下:
ListNode ptr0 = fakehead; //prev
ListNode ptr1 = fakehead.next;
//current
ListNode ptr2 = fakehead.next.next; //post
同时还需要引入一个布尔型的判断flag,来帮助判断当前是否遇到有重复,这个flag能帮助识别是否需要删除重复。
当没有遇到重复值(flag为false)时,3个指针同时往后移动:
ptr0 = ptr1;
ptr1 = ptr2;
ptr2 = ptr2.next;
当遇到重复值时,设置flag为true,并让ptr2一直往后找找到第一个与ptr1值不等的位置时停止,这时,ptr1指向的node的值是一个重复值,需要删除,所以这时就需要让ptr0的next连上当前的ptr2,这样就把所有重复值略过了。然后,让ptr1和ptr2往后挪动继续查找。
这里还需要注意的是,当ptr2一直往后找的过程中,是有可能ptr2==null(这种情况就是list的最后几个元素是重复的,例如1->2->3->3->null),这时ptr1指向的值肯定是需要被删除的,所以要特殊处理,令ptr0的next等于null,把重复值删掉。其他情况说明最后几个元素不重复,不需要处理结尾,遍历就够了。
代码如下:
1 public ListNode deleteDuplicates(ListNode head) {
2 if(head == null || head.next == null)
3 return head;
4
5 ListNode fakehead = new ListNode(0);
6 fakehead.next = head;
7
8 ListNode ptr0 = fakehead;
9 ListNode ptr1 = fakehead.next;
ListNode ptr2 = fakehead.next.next;
boolean flag = false;
while(ptr2!=null){
if(ptr1.val == ptr2.val){
flag = true;
ptr2 = ptr2.next;
if(ptr2 == null)
ptr0.next = null;
}else{
if(flag){
ptr0.next = ptr2;
flag = false;
}else{
ptr0 = ptr1;
}
ptr1 = ptr2;
ptr2 = ptr2.next;
}
}
return fakehead.next;
}
Remove Duplicates from Sorted List II leetcode java的更多相关文章
- Remove Duplicates from Sorted Array II leetcode java
题目: Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For e ...
- Remove Duplicates from Sorted Array II [LeetCode]
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- Remove Duplicates from Sorted Array II ——LeetCode
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- Remove Duplicates from Sorted List II [LeetCode]
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- Remove Duplicates from Sorted List II ——LeetCode
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
- LeetCode 82. 删除排序链表中的重复元素 II(Remove Duplicates from Sorted List II)
82. 删除排序链表中的重复元素 II 82. Remove Duplicates from Sorted List II 题目描述 给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中没有 ...
- 【leetcode】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- 【LeetCode练习题】Remove Duplicates from Sorted List II
Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...
随机推荐
- HLG2035广搜
Diablo Time Limit: 1000 MS Memory Limit: 65536 K Total Submit: 42(21 users) Total Accepted: 23(20 us ...
- PXE介绍(PXE+kickstart无人值守安装)
PXE概念 PXE(Pre-boot Execution Environment,预启动执行环境)是由Intel公司开发的最新技术,工作于Client/Server的网络模式,支持工作站通过网络从远端 ...
- Android 中的selector
今天做程序时,发现了selector 选择器不单单能用系统的自定义属性(比如, <item android:state_selected="true" android:co ...
- iOS CALayer动画中使用的3个tree
在网上经常看到关于layer的tree的描述,不太理解,今天找到了官方文档,原文在Core Animation Programming Guide 中. Layer Trees Reflect Dif ...
- 六间房PK同时观看两方视频(绕过VIP限制)+直播状态批量监测
可交换两个视频位置,记住最后播放记录,游客VIP限制也能观看视频等功能. 使用方法: 1.先运行 6.cn.live.exe 分别打开两个主播房间的网页(VIP限制也能获取视频的文件名) (房间已满提 ...
- mysql_1
1.mysql> select NOW();等效于select user()\g+---------------------+| NOW() |+---------- ...
- Android之Bundle类
API文档说明 1.介绍 用于不同Activity之间的数据传递 1.重要方法 clear():清除此Bundle映射中的所有保存的数据. clone():克隆当前Bundle containsKey ...
- redhat6.2下的ssh密钥免密码登录(原创)
这个是我自己写的,鼓励转载,请说明转载地址:http://www.cnblogs.com/nucdy/p/5664840.html 在进行hadoop的免密码的登录操作是,老是发生no route等错 ...
- 用例视图 Use Case View(rose)
找开Rose工具,选择用例视图 Use Case View 先看看这个视图下面都有哪些工具,都能做一些什么: 下面详细说一下: 用例视图下面有工具: 一:选择工具 二:文本框Text Box 三:注 ...
- p235习题2
List 成功添加 Set 添加失败