My Clean C# Solution


  • 0
    A
    public ListNode DeleteDuplicates(ListNode head) {
        
        ListNode prev = null;
        ListNode node = head;
        
        while (node != null)
        {
            if (node.next != null && node.val == node.next.val)
            {
                int duplicatedValue = node.val;
                
                do { node = node.next; } while (node.next != null && duplicatedValue == node.next.val);
                
                if (prev == null)
                    head = node.next;
                else
                    prev.next = node.next;
            }
            else
                prev = node;
                
            node = node.next;
        }
        
        return head;
    }

Log in to reply
 

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