LeetCode—83. Remove Duplicates from Sorted List

Type:easy

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


public:

    ListNode* deleteDuplicates(ListNode* head) {

        if(!head || !head->next) return head;

        ListNode* dummy = head;

        ListNode* cur = head;

        while(cur->next){

            if(cur->next->val != cur->val){

                dummy->next = cur->next;

                dummy = dummy->next;

                cur = cur->next;

            }else cur = cur->next;

        }

        dummy->next = NULL;

        return head;

    }

};

你可能感兴趣的:(LeetCode—83. Remove Duplicates from Sorted List)