My c++ adds a flag


  • 0
    Z

    class Solution {
    public:
    ListNode* deleteDuplicates(ListNode* head) {
    if (head == NULL) return NULL;
    if (head->next == NULL) return head;
    ListNode* pre = new ListNode(-1);
    pre->next = head; /// !!!
    ListNode* ret = pre;
    ListNode* p = head;
    while(p != NULL){
    bool flag = true;
    while( p->next != NULL && p->val == p->next->val ){
    p = p-> next;
    flag = false;
    }
    if (!flag){
    p = p->next;
    continue;
    }
    else if (flag){
    pre ->next = p;
    pre = p;
    p = p->next;
    }
    }
    pre -> next = NULL;
    return ret->next;
    }
    };


Log in to reply
 

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