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 ...
随机推荐
- Ubuntu上如何安装Java,Eclipse,Pydev,Python(自带,不用装),BeautifulSoup
如何安装Java,如果出于编程的需要安装Java,需要安装的是JDK,而不仅仅是JRE,下面说说如何在Ubuntu下如何安装JDK:只有两步,1.下载并解压,2.配置环境变量1.下载并解压:下载地址: ...
- Word Pattern | & II
Word Pattern | Given a pattern and a string str, find if str follows the same pattern. Examples: pat ...
- Solr5.3.1整合IKAnalyzer
由于solr5.3.1本身不支持中文分词,而msseg4j的分词效果不明显.因而采用IK进行分词,然而参考http://www.superwu.cn/2015/05/08/2134/在google上下 ...
- mysql 表空间
开启了Innodb的innodb_file_per_table这个参数之后[innodb_file_per_table = 1],也就是启用InnoDB的独立表空间模式,便于管理.此时,在新建的inn ...
- DP:Bridging Signals(POJ 1631)
不能交叉的引脚 (这一题的难度在于读题)题目大意:有一堆引脚(signals),左边一排,右边一排,左边从上到下,对应着连接右边的引脚(所有的引脚都被接上),现在引脚之间的连线有交叉,我们要桥接这些交 ...
- BestCoder12 1002.Help him(hdu 5059) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5059 题目意思:就是输入一行不多于 100 的字符串(除了'\n' 和 '\r' 的任意字符),问是否 ...
- [Android UI] shape和selector的结合使用
shape和selector是Android UI设计中经常用到的,比如我们要自定义一个圆角Button,点击Button有些效果的变化,就要用到shape和selector.可以这样说,shape和 ...
- dbvisualizer中文乱码
.dbvisualizer中文乱码 tools--tool properties找到Fonts--修改SQL Editor/Text Editor 将字体换成:微软雅黑.新宋体.楷体.黑体 .dbvi ...
- 大白菜U盘启动盘制作工具V5.0如何制作启动系统U盘
1:切换到ISO模式或者直接点击主程序左上角的ISO制作,程序会切换到ISO制作界面. 2:在路径里选好ios文件,点击按钮. 3:打开ISO模式的一键制作启动U盘,点击ISO模式里的按钮,按照图中推 ...
- Ubuntu 下搭建SVN服务器
root@iZ25q0jd99eZ:~# sudo apt-get install subversion root@iZ25q0jd99eZ:/etc/subversion# mkdir /svn r ...