diff --git a/1.linked-ist/remove-duplicates-from-sorted-list-II/memo.md b/1.linked-ist/remove-duplicates-from-sorted-list-II/memo.md new file mode 100644 index 0000000..d46dd04 --- /dev/null +++ b/1.linked-ist/remove-duplicates-from-sorted-list-II/memo.md @@ -0,0 +1,12 @@ +## step1 +- 出現が1回の要素から成る新しいリストを返す。 +- 時間計算量O(Nlog(N))、空間計算量O(N)。 +- mapのイテレーションは型が明確なのでautoを使う。 + +## step2 +- 線形に探索し、被りがあればheadを変更すると、時間計算量O(N)、空間計算量O(1)になることに気づき、実装する。 +- 用途などはあまり分からず、パフォーマンス要件の考察は断念 + - 「複数回呼び出される関数に見えるので、どちらの計算量は少ない方が良い」という当たり前の考察しかできなかった。 + +## step3 +- 3分ほどで実装。Step2でループごとの変数の動きをしっかり理解したため詰まらず実装できた。 \ No newline at end of file diff --git a/1.linked-ist/remove-duplicates-from-sorted-list-II/step1.cpp b/1.linked-ist/remove-duplicates-from-sorted-list-II/step1.cpp new file mode 100644 index 0000000..7e4288b --- /dev/null +++ b/1.linked-ist/remove-duplicates-from-sorted-list-II/step1.cpp @@ -0,0 +1,33 @@ +#include + +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) { + std::map cnts; + while (head) { + cnts[head->val]++; + head = head->next; + } + ListNode *deleted_head = NULL; + for (auto [n, c] : cnts) { + if (c > 1) continue; + if (!head) { + deleted_head = new ListNode(n); + head = deleted_head; + } + else { + head->next = new ListNode(n); + head = head->next; + } + } + return deleted_head; + } +}; \ No newline at end of file diff --git a/1.linked-ist/remove-duplicates-from-sorted-list-II/step2.cpp b/1.linked-ist/remove-duplicates-from-sorted-list-II/step2.cpp new file mode 100644 index 0000000..b1fcb12 --- /dev/null +++ b/1.linked-ist/remove-duplicates-from-sorted-list-II/step2.cpp @@ -0,0 +1,41 @@ +#include + +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) { + ListNode* seek = head; + ListNode* pre = NULL; + ListNode* border; + int sequence_cnt; + while (seek) { + border = seek; + sequence_cnt = 0; + while (border->next && border->val == border->next->val) { + border = border->next; + sequence_cnt++; + } + if (sequence_cnt == 0) { + pre = seek; + seek = seek->next; + continue; + } + + if (!pre) { + seek = head = border->next; + } + else { + pre->next = border->next; + seek = border->next; + } + } + return head; + } +}; \ No newline at end of file diff --git a/1.linked-ist/remove-duplicates-from-sorted-list-II/step3.cpp b/1.linked-ist/remove-duplicates-from-sorted-list-II/step3.cpp new file mode 100644 index 0000000..c8a1166 --- /dev/null +++ b/1.linked-ist/remove-duplicates-from-sorted-list-II/step3.cpp @@ -0,0 +1,41 @@ +#include + +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) { + ListNode* seek = head; + ListNode* pre = NULL; + ListNode* border; + int sequence_cnt; + while (seek) { + border = seek; + sequence_cnt = 0; + while (border->next && border->val == border->next->val) { + border = border->next; + sequence_cnt++; + } + if (sequence_cnt == 0) { + pre = seek; + seek = seek->next; + continue; + } + + if (!pre) { + seek = head = border->next; + } + else { + pre->next = border->next; + seek = border->next; + } + } + return head; + } +}; \ No newline at end of file