@@ -0,0 +1,1749 @@
+/***%0A * Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.%0A *%0A * For example,%0A * Given 1-%3E2-%3E3-%3E3-%3E4-%3E4-%3E5, return 1-%3E2-%3E5.%0A * Given 1-%3E1-%3E1-%3E2-%3E3, return 2-%3E3.%0A */%0A/**%0A * Definition for singly-linked list.%0A * public class ListNode %7B%0A *     int val;%0A *     ListNode next;%0A *     ListNode(int x) %7B%0A *         val = x;%0A *         next = null;%0A *     %7D%0A * %7D%0A*/%0Apublic class Solution %7B%0A    // Solution 1. Iterative: O(n), O(1)%0A    /*%0A    public ListNode deleteDuplicates(ListNode head) %7B%0A        if (head == null %7C%7C head.next == null)%0A            return head;%0A        ListNode ret = new ListNode(Integer.MIN_VALUE);%0A        ret.next = head;%0A        ListNode cur = ret.next;%0A        ListNode prev = ret;%0A        while(cur != null) %7B%0A            boolean duplicated = false;%0A            while(cur.next != null && cur.val == cur.next.val) %7B%0A                duplicated = true;%0A                cur = cur.next;%0A            %7D%0A            if (duplicated) %7B // Delete last duplicated node%0A                cur = cur.next;%0A                continue;%0A            %7D%0A            prev.next = cur;%0A            prev = prev.next;%0A            cur = cur.next;%0A        %7D%0A        prev.next = cur;%0A        return ret.next;%0A    %7D%0A    */%0A%0A    // Solution 2. Recursive: O(n), O(1)%0A    public ListNode deleteDuplicates(ListNode head) %7B%0A        if (head == null %7C%7C head.next == null)%0A            return head;%0A        ListNode p = head.next;%0A        if (head.val == p.val) %7B%0A            while(p != null && head.val == p.val)%0A                p = p.next;%0A            return deleteDuplicates(p);%0A        %7D else %7B%0A            head.next = deleteDuplicates(head.next);%0A            return head;%0A        %7D%0A    %7D %0A%7D%0A

add RemoveDuplicatesfromSortedListII.java