C# Solution


  • 0
    public ListNode DeleteDuplicates(ListNode head) {
            if (head == null || head.next == null)
                return head;
            
            ListNode dummyNode = new ListNode(-1), 
                     tempNode1, 
                     tempNode2, 
                     tempNode3;
                     
            dummyNode.next = head;
            tempNode1 = dummyNode;
            tempNode2 = tempNode1.next;
            tempNode3 = tempNode2.next;
            
            while (tempNode3 != null)
            {
                if (tempNode2.val == tempNode3.val)
                {
                    while(tempNode3 != null && tempNode2.val == tempNode3.val)
                    {
                        tempNode2 = tempNode3;
                        tempNode3 = tempNode2.next;
                    }
                    
                    tempNode2.next = null;
                    tempNode1.next = tempNode3;
                }
                else
                    tempNode1 = tempNode1.next;
                    
                if (tempNode1 != null)
                    tempNode2 = tempNode1.next;
                if (tempNode2 != null)
                    tempNode3 = tempNode2.next;
                else
                    tempNode3 = null;
            }
            
            return dummyNode.next;
        }

Log in to reply
 

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