My c solution. Any simple code?


  • 0
    L
    ListNode *deleteDuplicates(ListNode *head) {
        if (head == NULL || head->next == NULL) {
            return head;
        }
        ListNode null_node(0);
        ListNode* insert_node = &null_node;
        ListNode* pivot = head;
        int last_val = head->val - 1;
        while (pivot) {
            if (last_val != pivot->val && ((pivot->next == NULL) || (pivot->val != pivot->next->val))) {
                insert_node->next = pivot;
                insert_node = insert_node->next;                
            }
            last_val = pivot->val;
            pivot = pivot->next;
        }
        insert_node->next = NULL;
        return null_node.next;
    }

Log in to reply
 

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