Skip to content

141. Linked List Cycle #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions 141. Linked List Cycle.md
Original file line number Diff line number Diff line change
@@ -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`のチェックは不要なので削除。
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

下の2つの URL で Discord を漁ってみてください。
https://google.github.io/styleguide/pyguide.html#2144-decision
https://peps.python.org/pep-0008/#programming-recommendations:~:text=Also%2C%20beware%20of%20writing%20if%20x%20when%20you%20really%20mean%20if%20x%20is%20not%20None
結論はどちらでもいいんですが、相手を説得するときに何をもってよしあしを論じるか、趣味の範囲だが周りと合わせるという態度、が大事だと思います。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ありがとうございます!確認します。

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ある種の「他者の発見」という話なのだろうと思っています。

自分の外に規範があって、それとの距離をどうするかという話です。

- 参考: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
```