C# accepted solution


  • 0
    R
    public static ListNode DeleteDuplicates (ListNode head) {
        if (head == null || head.next == null) return head;
        ListNode temp = head;
        ListNode dummy = new ListNode (Int32.MinValue);
        dummy.next = head;
        ListNode prev = dummy;
        while (temp != null && temp.next != null)
    	{
            if (temp.val == temp.next.val)
    		{
                var data = temp.val;
                while (temp != null && temp.val == data)
    			{
    				 temp = temp.next;
    		    }
                //move pointer to the next element after the last duplicate and update prev
                prev.next = temp;
            } 
    		else
    		{
                prev.next = temp;
                //update previous in case of no dupes
                prev = temp;
                if (temp != null) temp = temp.next;
            }
        }
        return dummy.next;
    }

Log in to reply
 

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