Simple and easy understand(10 lines, c#, java)


  • 0
    J
    public class Solution {
        public ListNode deleteDuplicates(ListNode head) {
            ListNode dummy = new ListNode(0), pre = dummy, next;
            pre.next = head;
            while (pre.next != null){
                next = pre.next.next;
                while (next != null && pre.next.val == next.val) next = next.next;    // collect all duplicates
                if (next == pre.next.next)
                    pre = pre.next;     // no duplicates
                else
                    pre.next = next;      // delete all duplicates
            }
            return dummy.next;
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.