LC 83. Remove Duplicates from Sorted List

Nilanjan Deb · April 5, 2020

This is my CPP solution.

class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        ListNode* origHead = head;
        while(head){
            ListNode* later = head->next;
            while(later && (later->val == head->val)){
                later = later->next;
            }
            if(!later){
                head->next = NULL;
                break;
            }
            else{
                head->next = later;
            }
            head = head->next;
        }
        return origHead;
    }
};


Dicussion Forum