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 ...
随机推荐
- 暑假热身 E. 无聊的LSY
LSY大牛没事就爱玩游戏,包括很多很无聊的游戏.某日,LSY大牛又找到了一个无聊的游戏:每一局游戏的开始,LSY大牛将代表自己的棋子放在一个线性棋盘的最左端(第0个格子,可以认为向右端无限延伸),接着 ...
- typedef 各类定义,各类问题大全
第一篇:typedef struct与struct的区别 1. 基本解释 typedef为C语言的关键字,作用是为一种数据类型定义一个新名字.这里的数据类型包括内部数据类型(int,char等)和自定 ...
- django-cms 代码研究(七)杂七杂八
实体关系图 核心对象: cms_page/cms_placeholder/cms_cmsplugin. page模型类继承关系图 CMSPlugin&Placeholder模型类继承关系图 = ...
- Java--时间处理
package javatest; import java.text.SimpleDateFormat; import java.util.Date; class timeTest{ public s ...
- 一段功能齐全的PHP常用重定向代码html+js+header
/** * 重定向浏览器到指定的 URL * * @param string $url 要重定向的 url * @param int $delay 等待多少秒以后跳转 * @param bool $j ...
- 处理html5离线应用程序存储的一些问题。
manifest方法引入appcache文件,缓存页面,是html5的新特性,通过加载一次,下次自动读取缓存,加载速度快,离线也能加载.缺点就是,被加载的页面会被强制缓存所有的内容. 为了解决不加载所 ...
- ubuntu使用root账户登录
1.先设定一个root的密码 sudo passwd root 2.编辑lightdm.conf sudo gedit /etc/lightdm/lightdm.conf 最后一行添加 greeter ...
- JDK 工具列表
jar — 一个创建和管理 jar 文件的工具. java — Java 应用启动器.在这篇文章里,开发和部署都是用的这个启动器. javac — Java 编译器. javadoc — API 文档 ...
- 归并排序(merge sort)
M erge sort is based on the divide-and-conquer paradigm. Its worst-case running time has a lower ord ...
- lucene 搜索引擎使用案例
1.使用定时框架Quartz.Net创建索引库,引用类库文件有Common.Logging.dll.Lucene.Net.dll,PanGu.dll,PanGu.HighLight.dll,PanGu ...