c++ simple solution


  • 0
    H
    class Solution {
    public:
        ListNode* deleteDuplicates(ListNode* head) {
            if(!head || !head->next) return head;
            ListNode* new_head = new ListNode(0);
            new_head->next = head;
            ListNode* pre = new_head, * cur = head;
            while(cur && cur->next) {
                if(cur->val != cur->next->val) {
                    pre = pre->next;
                    cur = cur->next;
                }
                else {
                    while(cur->next && cur->val == cur->next->val) cur = cur->next;
                    pre->next = cur->next;
                    cur = cur->next;
                }
            }
            return new_head->next;
        }
    };
    

Log in to reply
 

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