-
Notifications
You must be signed in to change notification settings - Fork 0
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
katataku
wants to merge
2
commits into
main
Choose a base branch
from
141.-Linked-List-Cycle
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`のチェックは不要なので削除。 | ||
- 参考: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 | ||
``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
結論はどちらでもいいんですが、相手を説得するときに何をもってよしあしを論じるか、趣味の範囲だが周りと合わせるという態度、が大事だと思います。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ありがとうございます!確認します。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ある種の「他者の発見」という話なのだろうと思っています。
自分の外に規範があって、それとの距離をどうするかという話です。