Posted on

Description

Submission

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        if(!head || !head->next) return head;
        head = new ListNode(head->val+1, head);
        ListNode* prevNode = head;
        for(ListNode* node = head->next; node != nullptr; node = node->next) {
            if(prevNode->val == node->val) {
                prevNode->next = node->next;
                delete node;
                node = prevNode;
            } else {
                prevNode = node;
            }
        }
        head = head->next;
        return head;
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *