Using two star pointer


  • 0
    W
    class Solution {
    private:
        void helper(ListNode **head) {
            for (ListNode **cur = head; (*cur)->next != nullptr;) {
                ListNode *entry = *cur;
                if (entry->val == entry->next->val) {
                    *cur = entry->next;
                    delete entry;
                } else {
                    cur = &(entry->next);
                }
            }
        }
    public:
        ListNode* deleteDuplicates(ListNode* head) {
            if (head == nullptr)
                return nullptr;
            helper(&head);
            return head;
        }
    };

Log in to reply
 

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