From cfa3e720aa1c4b983bec546b5c6879d6d570b54a Mon Sep 17 00:00:00 2001 From: ntanaka1984 Date: Tue, 15 Jul 2025 18:19:58 +0900 Subject: [PATCH 1/2] Create memo.md --- 142. Linked List Cycle II/memo.md | 88 +++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 142. Linked List Cycle II/memo.md diff --git a/142. Linked List Cycle II/memo.md b/142. Linked List Cycle II/memo.md new file mode 100644 index 0000000..ea4a0d5 --- /dev/null +++ b/142. Linked List Cycle II/memo.md @@ -0,0 +1,88 @@ +# 142. Linked List Cycle II + +https://leetcode.com/problems/linked-list-cycle-ii/description/ + +与えられたリストが閉路を含む場合、閉路の始点となるノードを返す。閉路がない場合はNULLを返す。 +## 実装方針 + +141 Linked List Cycle I と同様にListNode のポインタをset に格納し、再度訪れるかをチェックする。再訪した場合は格納されていたListNode のポインタを返す。 + +### 1st step +* 141のレビューを頂く前に書いたものなので`contains`を使わずに`find()`と`end()`の一致で要素がmap にあるかを調べてしまっています。 +* ListNode ポインタの情報を格納するのはset にするべきでしたが、始点がリストの何番目にあるのかという情報が必要になるかな?と思い`map`に格納してしまいました。 + +```c++ +#include +class Solution { +public: + ListNode *detectCycle(ListNode *head) { + ListNode* node = head; + std::map node_to_pos; + int pos = 0; + while (node != NULL) { + auto it = node_to_pos.find(node); + if (it != node_to_pos.end()) { + return(it->first); + } + node_to_pos.emplace(node, pos++); + node = node->next; + } + return NULL; + } +}; +``` + +## 2nd step + +### 141. Linked List Cycle I の指摘を受けて以下の項目を修正 + + +* set の要素の有無をcontains()で調べるようにしました。 +* node がNULL であるかの判定を`while (node)` で行うようにしました。 +* NULL を返すところを`return nullptr;`で返すようにしました。 + +```c++ +#include +class Solution { +public: + ListNode *detectCycle(ListNode *head) { + ListNode* node = head; + std::set visited; + while (node) { + if (visited.contains(node)) { + return node; + } + visited.insert(node); + node = node->next; + } + return nullptr; + } +}; + +``` + +### 3rd step + +2nd step と変化ありません。 + +所要時間 1分17秒です。 + +```c++ +#include +class Solution { +public: + ListNode *detectCycle(ListNode *head) { + ListNode* node = head; + std::set visited; + while (node) { + if (visited.contains(node)) { + return node; + } + visited.insert(node); + node = node->next; + } + return nullptr; + } +}; + +``` From 56075b6827b00cbb6d926634686fe7d413674753 Mon Sep 17 00:00:00 2001 From: ntanaka1984 Date: Tue, 15 Jul 2025 18:59:54 +0900 Subject: [PATCH 2/2] Update 4th step's code in memo.md --- 142. Linked List Cycle II/memo.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/142. Linked List Cycle II/memo.md b/142. Linked List Cycle II/memo.md index ea4a0d5..6c9717a 100644 --- a/142. Linked List Cycle II/memo.md +++ b/142. Linked List Cycle II/memo.md @@ -86,3 +86,32 @@ public: }; ``` + +### 4th step + +野田さんの以下の指摘を見て、while の前後に空行を挿入しました。141 では指摘されていなかったので必要ないかもしれませんが… + +https://github.com/mptommy/coding-practice/pull/2/files/3f4ec18956cde81139bf79c18a575849545645c4#r2126273567 + +```c++ +#include +class Solution { +public: + ListNode *detectCycle(ListNode *head) { + ListNode* node = head; + std::set visited; + + while (node) { + if (visited.contains(node)) { + return node; + } + visited.insert(node); + node = node->next; + } + + return nullptr; + } +}; +``` + +