C++ solution using dummy node easy to understand


  • 0
    S

    below is my easy to understand solution using dummy node, can anyone tell me how to free the dummy node please?

    ListNode* deleteDuplicates(ListNode* head) {
            ListNode* dummy = new ListNode(0);
            dummy->next = head;
            //ListNode* cur = dummy;
            head = dummy;
            while(head->next && head->next->next) {
            	if(head->next->val == head->next->next->val) {
            		int value = head->next->val;
            		while(head->next && head->next->val == value) {
            			head->next = head->next->next;
            		}
            	} else {
            		head = head->next;
            	}
            }
            return dummy->next;
        }

Log in to reply
 

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