C++/C# simple solution


  • 1
    O

    C++ version

    ListNode* deleteDuplicates(ListNode* head) {
        ListNode* node = head;
        while(node != NULL){
           if((node->next != NULL) && (node->next->val == node->val))
           {
               ListNode* oldNext = node->next;
               node->next = node->next->next;
               delete oldNext;
           }
           else
               node = node->next;
       } 
       return head;
    }
    

    C# version

    public ListNode DeleteDuplicates(ListNode head) {
         ListNode node = head;
        while(node != null){
           if((node.next != null) && (node.next.val == node.val))
               node.next = node.next.next;
           else
               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.