C# - Iterative


  • 0
    public ListNode DeleteDuplicates(ListNode head) {
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        
        ListNode curr = dummy;
        ListNode check = head;
        while (check != null)
        {
            int val = check.val;
            check = check.next;
            bool dupe = false;
            while (check != null && check.val == val)
            {
                dupe = true;
                check = check.next;
            }
            
            if (dupe)
            {
                curr.next = check;
            }
            else
            {
                curr = curr.next;
            }
        }
        
        return dummy.next;
    }

Log in to reply
 

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