From 66c304778c45cf881d127be78d209f4d8ea036bb Mon Sep 17 00:00:00 2001 From: katataku Date: Thu, 7 Nov 2024 14:27:36 +0900 Subject: [PATCH 1/2] 141. Linked List Cycle.md --- 141. Linked List Cycle.md | 82 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 141. Linked List Cycle.md diff --git a/141. Linked List Cycle.md b/141. Linked List Cycle.md new file mode 100644 index 0000000..2c0e591 --- /dev/null +++ b/141. Linked List Cycle.md @@ -0,0 +1,82 @@ + +URL: https://leetcode.com/problems/linked-list-cycle/description/ + +# Step 1 + +- 実装時間: 5分 +- 時間計算量: O(n) +- 空間計算量: O(1) +- Fast-Slowという単語を聞いたことがあったので、思い出しながら5分ほどで実装。 +- 空のリストのケースを考慮できておらず一度NGとなった。 + +```python +class Solution: + def hasCycle(self, head: Optional[ListNode]) -> bool: + fast, slow = head, head + while fast is not None and fast.next is not None and fast.next.next is not None: + fast = fast.next.next + slow = slow.next + if fast == slow: + return True + return False +``` + +# Step 2 + +- 代入のスタイルの一貫性がないことに気づいた。「多重代入」か「それぞれ代入する」か。今回は、「それぞれ代入する」に統一する。 +- ループ条件のfast.next.nextは確認する意味がないことに気づいたので、削除。 +- Noneじゃないことのチェックに、`is not None`のチェックは不要なので削除。 + - 参考:https://docs.python.org/3/library/stdtypes.html#truth-value-testing + +```python +class Solution: + def hasCycle(self, head: Optional[ListNode]) -> bool: + fast = head + slow = head + while fast and fast.next: + fast = fast.next.next + slow = slow.next + if fast == slow: + return True + return False +``` + +- サンプル回答を見ていて別解を見つけた。こっちの方が優れていると感じた。 + - fast-slowというアイデアを使わずに、訪問済みのNodeをチェックする素直な実装。 +- デメリット + - visitedが大きくなるので、空間計算量がO(n)となる。 + - `if curr in visited:`で時間計算量O(n)のチェックが必要。 + +```python +class Solution: + def hasCycle(self, head: Optional[ListNode]) -> bool: + visited = set() + + curr = head + + while curr: + if curr in visited: + return True + visited.add(curr) + curr = curr.next + + return False +``` + +# Step 3 + +- 時間計算量: O(n) +- 空間計算量: O(n) + +```python +class Solution: + def hasCycle(self, head: Optional[ListNode]) -> bool: + visited = set() + cur = head + while cur: + if cur in visited: + return True + visited.add(cur) + cur = cur.next + return False +``` From cd4914ec3e8eaf14ed21aac0351ad144c5f1591b Mon Sep 17 00:00:00 2001 From: katataku Date: Thu, 7 Nov 2024 14:32:35 +0900 Subject: [PATCH 2/2] =?UTF-8?q?fix/=E6=9B=B8=E5=BC=8F=E3=81=AE=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 141. Linked List Cycle.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/141. Linked List Cycle.md b/141. Linked List Cycle.md index 2c0e591..2d998e1 100644 --- a/141. Linked List Cycle.md +++ b/141. Linked List Cycle.md @@ -4,7 +4,7 @@ URL: https://leetcode.com/problems/linked-list-cycle/description/ # Step 1 - 実装時間: 5分 -- 時間計算量: O(n) +- 時間計算量: O(n) - 空間計算量: O(1) - Fast-Slowという単語を聞いたことがあったので、思い出しながら5分ほどで実装。 - 空のリストのケースを考慮できておらず一度NGとなった。 @@ -65,7 +65,7 @@ class Solution: # Step 3 -- 時間計算量: O(n) +- 時間計算量: O(n) - 空間計算量: O(n) ```python