10-Line C# Solution Time O(N) Space O(1)


  • 0
    L
    public ListNode DeleteDuplicates(ListNode head) {
        ListNode dummy = new ListNode(0), pre = dummy, cur = pre.next;
        pre.next = head;
        while(pre.next != null && pre.next.next != null)
            if(pre.next.val == pre.next.next.val){
                cur = pre.next.next;
                while(cur.next != null && cur.next.val == cur.val) cur = cur.next;
                pre.next = cur.next;
            }
            else pre = pre.next;
        return dummy.next;
    }

Log in to reply
 

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