sundeepblue
4/27/2014 - 2:18 AM

given a sorted linked list, delete all duplicates such that each element appear only once.

given a sorted linked list, delete all duplicates such that each element appear only once.

ListNode* remove_duplicates(ListNode *head) {
    if(!head || !head->next) return head;
    ListNode *cur = head;
    while(cur) {
        ListNode *nxt = cur->next;
        if(nxt == NULL) return head; 
        if(cur->val == nxt->val) {
            ListNode *nnxt = nxt->next;
            delete nxt;
            cur->next = nnxt;
            nxt = nnxt;
        } else {
            cur = cur->next;
        }
    }
    return head;
}