-
Notifications
You must be signed in to change notification settings - Fork 0
142. Linked List Cycle II.md #5
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?
Conversation
|
||
なにはともあれ、フロイドの循環検出も実装してみる。 | ||
|
||
- ヘルパー関数を使った方がわかりやすい気がした。 |
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.
構造の前後を入れ替えたいときに、
https://discord.com/channels/1084280443945353267/1221030192609493053/1225674901445283860
- 関数化。
- Python 独特の文法。
- 無限ループを使う。
などの方法があります。
def find_meeting_point(head: Optional[ListNode]) -> Optional[ListNode]: | ||
fast = head | ||
slow = head | ||
meeting_node = None |
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.
使っていないですね。
return slow | ||
return None | ||
|
||
fast = find_meeting_point(head) |
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.
ここでの変数名がfast
なのはちょっと気になります。まあ好みの範囲かもしれないですが、うさぎが亀に追いついた後は、どちらも1つずつしか進まないのでslow, fastという命名はちょっと違うなという気持ちがあります。
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.
ありがとうございます。たしかに・・・
他の方の書き方を見てみたので、まとめ:
- node1, node2とやっているパターン
- from_headという書き方のパターン。
- 142. Linked List Cycle II colorbox/leetcode#18
- from_headと合わせるなら、from_meeting_pointにするとbetterかと思った。とはいえ、slowが一歩づつというのは変わっていない情報なので、あまりこだわらなくていいかも。
while fast is not None and fast.next is not None: | ||
fast = fast.next.next | ||
slow = slow.next | ||
if slow == fast: | ||
return slow | ||
return None |
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.
whileの条件分岐はfastを使っていてnodeの更新もfastからするなら、ifの条件の左辺をfastにしてreturnで返す値もfastにしないのかな~と思いました。
処理は全く変わらないですが、なんとなく気になったところです。
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. Add Two Numbers.md #4 (comment)
- https://qiita.com/momopoketto7/items/5d41bbc2a98a8380d1b6
https://leetcode.com/problems/linked-list-cycle-ii/
URLを間違えていたので修正しました。