题目:

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的更多相关文章

  1. Remove Duplicates from Sorted Array II leetcode java

    题目: Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For e ...

  2. Remove Duplicates from Sorted Array II [LeetCode]

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  3. Remove Duplicates from Sorted Array II ——LeetCode

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  4. Remove Duplicates from Sorted List II [LeetCode]

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  5. Remove Duplicates from Sorted List II ——LeetCode

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  6. LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>

    LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...

  7. LeetCode 82. 删除排序链表中的重复元素 II(Remove Duplicates from Sorted List II)

    82. 删除排序链表中的重复元素 II 82. Remove Duplicates from Sorted List II 题目描述 给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中没有 ...

  8. 【leetcode】Remove Duplicates from Sorted Array II

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  9. 【LeetCode练习题】Remove Duplicates from Sorted List II

    Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...

随机推荐

  1. java常见异常集锦

    1. java.lang.nullpointerexception 这个异常大家肯定都经常遇到,异常的解释是"程序遇上了空指针",简单地说就是调用了未经初始化的对象或者是不存在的对 ...

  2. 速度之王 — LZ4压缩算法(三)

    LZ4使用 make / make clean 得到可执行程序:lz4.lz4c Usage: ./lz4 [arg] [input] [output] input : a filename Argu ...

  3. HTML模仿桌面

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. 4.在二元树中找出和为某一值的所有路径[FindPathsInBinaryTree]

    [题目]: 输入一个整数和一棵二元树.从树的根结点开始往下访问一直到叶结点所经过的所有结点形成一条路径.打印出和与输入整数相等的所有路径. 例如输入整数22和如下二元树 10              ...

  5. mysql 基于lvm快照的备份

    1.查看磁盘数 ls /dev/ | grep sd 2.快照备份 pvcreate /dev/sdb #制作成物理卷vgcreate testvg /dev/sdblvcreate -L200M - ...

  6. Javascript配合jQuery实现流畅的前端验证

    做前端时一般都习惯用JavaScript进行表单的简单验证比如非空验证和正则表达式验证,这样过滤后的数据提交到服务端再由专门的控制器做数据处理,这样能减轻服务器的负担,下面看一下前端验证的简单步骤: ...

  7. Python多线程(1)——介绍

    Python对多线程提供了很好的支持,Python中多线程相关的模块包括:thread,threading,Queue.可以方便地支持创建线程.互斥锁.信号量.同步等特性. 1. thread:多线程 ...

  8. 【JAVA、C++】LeetCode 015 3Sum

    Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...

  9. BestCoder17 1002.Select(hdu 5101) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5101 题目意思:给出 n 个 classes 和 Dudu 的 IQ(为k),每个classes 都有 ...

  10. HDU 2588 GCD (欧拉函数)

    GCD Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Status De ...