-
Notifications
You must be signed in to change notification settings - Fork 0
141. Linked List Cycle #2
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# step1 | ||
```python | ||
class Solution: | ||
def hasCycle(self, head: Optional[ListNode]) -> bool: | ||
if head == None: | ||
return False | ||
fast = head | ||
slow = head | ||
while fast != None: | ||
if fast.next != None: | ||
fast = fast.next.next | ||
else: | ||
return False | ||
slow = slow.next | ||
if fast == slow: | ||
return True | ||
return False | ||
``` | ||
#### 思考ログ | ||
- LinkedListというデータ構造をよく理解できていなかった | ||
- 普段のコーディングでポインタという概念をあまり意識していないなと思った | ||
- わからなかったのであらいさんの回答をみた | ||
|
||
# step2 | ||
```python | ||
class Solution: | ||
def hasCycle(self, head: Optional[ListNode]) -> bool: | ||
if head == None : | ||
return False | ||
fast = slow = head | ||
while fast != None and fast.next != None: | ||
fast = fast.next.next | ||
slow = slow.next | ||
if fast == slow: | ||
return True | ||
return False | ||
``` | ||
#### 思考ログ | ||
- 代入する部分を省略 | ||
- whileの中のifで特に処理がなくFalseを返していたのでwhileにまとめる | ||
|
||
# step3 | ||
```python | ||
class Solution: | ||
def hasCycle(self, head: Optional[ListNode]) -> bool: | ||
if head == None : | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. ああ、なるほど。行数が短くなるので削ってもいいと思います。趣味の範囲ですね。 |
||
return False | ||
fast = slow = head | ||
while fast != None and fast.next != None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. None との比較は is, is not が好まれますね。 そのまま条件にするのも一つです。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
なるほど、Pythonでは同値性が==で同一性がisなんですね!
確かに今回はそれで条件として問題なさそう&人間の認知的に否定は避けたほうがいいというのを聞いたことがあるのでそちらでも良さそうですね👀
とあったので、何も無い時はNoneと明示的に指定したほうがいいのかなとも思いましたがいかがでしょう? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PEP と Google Style Guide を引用しておきます。 https://peps.python.org/pep-0008/#programming-recommendations
https://google.github.io/styleguide/pyguide.html#2144-decision
ここで大事なのは、相手を説得するときに何をもってよしあしを論じるかです。結論はどちらでも構いません。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 同一性(identity)と同値性(equality)、知らなかったので勉強になりました。 |
||
fast = fast.next.next | ||
slow = slow.next | ||
if fast == slow: | ||
return True | ||
return False | ||
``` | ||
#### 思考ログ | ||
- step2と同じ | ||
- 回答わかっているのになぜ3回連続なんだろうと思ったが、確かに詰まっているところはしっかり理解しきれていないところだと思った。 | ||
|
||
# step4 | ||
```python | ||
class Solution: | ||
def hasCycle(self, head: Optional[ListNode]) -> bool: | ||
if head is None : | ||
return False | ||
fast = slow = head | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 好みの問題ですが、自分だったらslow = fast = head (小さい、遅い方を先に持ってくる)の書き方にするかと思います。 |
||
while fast is not None and fast.next is not None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 可読性を考えると、while fast and fast.nextで良いと思いました。 |
||
fast = fast.next.next | ||
slow = slow.next | ||
if fast is slow: | ||
return True | ||
return False | ||
``` | ||
|
||
#### ハッシュテーブル | ||
```python | ||
class Solution: | ||
def hasCycle(self, head: Optional[ListNode]) -> bool: | ||
visited_nodes = set() | ||
current_node = head | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 好みですが、今回コードが短くスコープが狭いので、visited, currentでも良いかなと思いました。 |
||
while current_node: | ||
if current_node in visited_nodes: | ||
return True | ||
visited_nodes.add(current_node) | ||
current_node = current_node.next | ||
return False | ||
``` | ||
|
||
#### 思考ログ | ||
- レビューで指摘された点を修正 | ||
- ハッシュテーブルの回答もみて写経してみた | ||
- こちらのほうが思いつきやすそうだなという印象 |
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.
5分考えて全然わからなかったのであらいさんの回答を見ました🙏
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.
なるほど、承知しました!
次回から記載するようにします🙏