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 ...
随机推荐
- 微信开发学习日记(八):7步看懂weiphp插件机制,核心目标是响应微信请求
又经过了几个小时的梳理.回顾,截至目前,终于对weiphp这个框架的机制搞明白了些.想要完全明白,自然还需要大把的时间.第1步: 配置微信公众号,http://weiphp.jiutianniao ...
- Windows/Centos安装GO语言环境
转载:http://www.haiyun.me/archives/1009.html Centos下使用epel源安装: 1 yum install golang Centos/Linux下源码安装g ...
- Linux jstack命令详解
jstack用于打印出给定的java进程ID或core file或远程调试服务的Java堆栈信息. 如果是在64位机器上,需要指定选项"-J-d64",Windows的jstack ...
- 【Network】一张图看懂 Reactor 与 Proactor 模型的区别
首先来看看Reactor模式,Reactor模式应用于同步I/O的场景.我们以读操作为例来看看Reactor中的具体步骤:读取操作:1. 应用程序注册读就需事件和相关联的事件处理器2. 事件分离器等待 ...
- Ninject学习笔记<一>
本文转载自永远的阿哲 如果给您带来不便请联系博主. Ninject是一款.Net平台下的开源依赖注入框架.按照官方说法,它快如闪电.超级轻量,且充分利用了.Net的最新语法,使用Lambda表达式代替 ...
- MyBatis3: There is no getter for property named 'code' in 'class java.lang.String'
mybatis3 : mysql文如下,传入参数为string类型时‘preCode’,运行报错为:There is no getter for property named 'preCode' i ...
- 22.整数二进制表示中1的个数[Get1BitCount]
[题目] 输入一个整数,求该整数的二进制表达中有多少个1.例如输入10,由于其二进制表示为1010,有两个1,因此输出2. [分析] 如果一个整数不为0,那么这个整数至少有一位是1.如果我们把这个整数 ...
- codeforces 488A. Giga Tower 解题报告
题目链接:http://codeforces.com/problemset/problem/488/A 题目意思:给出一个数a,范围是[-10^9, 10^9],问它最少需要加的一个正整数 b 是多少 ...
- struts2 <s:property/>标签的使用--输出时间格式转换
转载地址http://hi.baidu.com/nonyi_com/blog/item/acf1b8d74b6cf63e07088bc4.html 最近在使用struts2的<s:propert ...
- 查看运行的KVM的虚机
[root@ok Desktop]# cat demo.py #!/usr/bin/python import libvirt conn = libvirt.open("qemu:///sy ...