zoukankan      html  css  js  c++  java
  • Remove Duplicates from Sorted List II

    描述
    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.
    分析

    代码

     1 public static ListNode deleteDuplicates2(ListNode head) {
     2         if (head == null || head.next == null) 
     3             return head;
     4         
     5         int t = Integer.MIN_VALUE;
     6         ListNode fakehead = new ListNode(t);// 加了个头指针
     7         fakehead.next = head;
     8         ListNode ptr0 = fakehead;
     9         ListNode ptr1 = fakehead.next;
    10         ListNode ptr2 = fakehead.next.next;
    11         boolean flag = false;
    12         while (ptr2 != null) {
    13             if (ptr1.data == ptr2.data) {
    14                 flag = true;
    15                 ptr2 = ptr2.next;
    16                 ptr1 = ptr1.next;
    17                 if (ptr2 == null)
    18                     ptr0.next = null;
    19             } else {
    20                 if (flag) {
    21                     ptr0.next = ptr2;
    22                     flag = false;
    23                     ptr1 = ptr2;
    24                     ptr2 = ptr2.next;
    25                 } else {
    26                     ptr0 = ptr0.next;
    27                     ptr1 = ptr2;
    28                     ptr2 = ptr2.next;
    29                 }
    30             }
    31         }
    32         return fakehead.next;
    33     }
  • 相关阅读:
    解决CollectionView TableView reloadData或者reloadSections时的刷新的闪烁问题
    HTTP请求头
    Fastlane 使用笔记
    python-函数式编程
    python-高级特性
    python基础使用
    python基础-函数02
    python基础-函数01
    python基础
    Linux基础
  • 原文地址:https://www.cnblogs.com/ncznx/p/9297118.html
Copyright © 2011-2022 走看看