82. Remove Duplicates from Sorted List II

weixin_34119545發表於2015-04-18

題目:

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.

連結: http://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/

題解:

連結串列去重II,這裡要建立一個fake head,因為假如全部為重複,需要移除所有的元素。 還需要一個boolean變數來判斷當前狀態是否重複。最後判斷迴圈結束時的邊界狀態。

Time Complexity - O(n), Space Complexity - O(1)。

public class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        if(head == null || head.next == null)
            return head;
        ListNode dummy = new ListNode(-1);
        ListNode node = dummy;
        boolean isDuplicate = false;
        
        while(head.next != null) {
            if(head.next.val == head.val)
                isDuplicate = true;
            else {
                if(!isDuplicate) {
                    node.next = head;
                    node = node.next;
                } else
                    isDuplicate = false;
            }
            head = head.next;
        }
        
        if(isDuplicate)
            node.next = null;
        else
            node.next = head;
        
        return dummy.next;
    }
}

 

二刷:

我發現自己的思路就是和自己的思路一樣...磨蹭了半天,二刷還是寫了跟一刷很類似的code....

我們主要就是用一個boolean hasDuplicate來記錄之前是否出現過重複,以及一個dummy節點來保證假如連結串列頭有重複我們也可以處理。

  1. 先做邊界判斷
  2. 建立fake head dummy, 以及 node = dummy
  3. 在head != null以及 head.next != null的條件下我們進行遍歷
  4. 假如head.val == head.next.val, 我們判定hasDuplicate = true
  5. 否則head.val != head.next.val,這時候我們要進行分析
    1. 假如hasDuplicate =false,這時候我們這個head可以加入到結果之中去,我們執行node.next = head, node = node.next
    2. 否則我們不管
    3. 這時候重置hasDuplicate = false
  6. 每次head = head.next
  7. 最後判斷最後一個元素,hasDuplicate為真時,我們把node.next設定為null,跳過最後一個重複元素, 否則node.next = head,返回結果

Java:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode dummy = new ListNode(-1);
        ListNode node = dummy;
        boolean hasDuplicate = false;
        while (head != null && head.next != null) {
            if (head.val == head.next.val) {
                hasDuplicate = true;
            } else {
                if (!hasDuplicate) {
                    node.next = head;
                    node = node.next;
                }
                hasDuplicate = false;
            }
            head = head.next;
        }
        node.next = hasDuplicate ? null : head;
        return dummy.next;
    }
}

 

三刷:

思路跟上面都差不多

Java:

Time Complexity - O(n), Space Complexity - O(1)。

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        if (head == null || head.next == null) return head;
        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        ListNode node = dummy;
        int count = 1;
        while (head != null && head.next != null) {
            if (head.val != head.next.val) {
                if (count == 1) {
                    node.next = head;
                    node = node.next;
                }
                count = 1;
            } else {
                count++;
            }
            head = head.next;
        }
        node.next = (count == 1) ? head : null;
        return dummy.next;
    }
}

 

相關文章