My c solution 5ms without memory leak


  • 0
    B
    struct ListNode* deleteDuplicates(struct ListNode* head) {
        if (head == NULL) return NULL;
        if (head->next == NULL) return head;
        struct ListNode node;
        struct ListNode *p1 = &node, *p2 = head, *tmp;
        int last = p2->val + 1, tmp_val;
        while (p2) {
            tmp = p2->next;
            tmp_val = p2->val;
            if (p2->val != last && (p2->next == NULL || p2->val != p2->next->val)) {
                p1->next = p2;
                p1 = p2;
            } else {
                free(p2);
            }
            last = tmp_val;
            p2 = tmp;
        }
        p1->next = NULL;
        return node.next;
    }

Log in to reply
 

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