코테준비 - Remove Duplicates from Sorted List II

정상화·2023년 2월 26일

LeetCode

목록 보기
79/222

Remove Duplicates from Sorted List II

class Solution {
public:
    ListNode *deleteDuplicates(ListNode *head) {
        int mem = -500;
        vector<ListNode*> nodes;
        auto fix = head;
        if(fix== nullptr) return nullptr;
        for (auto cur = head -> next; cur != nullptr; mem=fix->val, fix = cur, cur = cur->next) {
            if (fix->val != cur->val && mem != fix->val) {
                nodes.push_back(fix);
            }
        }
        if (mem != fix->val) {
            nodes.push_back(fix);
        }
        if(nodes.empty()) return nullptr;
        auto it = nodes.begin();
        for (; it + 1 != nodes.end(); it++) {
            (*it)->next = *(it + 1);
        }
        (*it)->next = nullptr;
        return nodes.front();
    }
};
profile
백엔드 희망

0개의 댓글