관리 메뉴

sleepyotter

Remove Duplicates from Sorted List 본문

프로그래밍/알고리즘+코딩테스트

Remove Duplicates from Sorted List

sleepyotter. 2021. 9. 18. 00:03

Head에서부터 순회하며 내 앞의 녀석이 나와 값이 같지 않을때까지, 나의next = 내앞의next 로 바꿔주면 된다.

class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        if(head==nullptr) return head;
        ListNode* cur = head;
        while (cur==nullptr || cur->next != nullptr)
        {
            while (cur->next->val == cur->val)
            {
                ListNode* temp = cur->next;
                cur->next = cur->next->next;
                if (cur->next == nullptr) break;
            }
            cur = cur->next;
            if (cur==nullptr) break;
        }

        return head;
    }
};

'프로그래밍 > 알고리즘+코딩테스트' 카테고리의 다른 글

Binary Tree Inorder Traversal  (0) 2021.09.19
Merge Sorted Array  (0) 2021.09.18
Climbing Stairs  (0) 2021.09.11
Sqrt(x)  (0) 2021.09.11
Plus One  (0) 2021.09.07