Conversation
| ``` | ||
|
|
||
| 新井浩平氏の解説動画を見ると、初見では理解できませんでした。 | ||
| `fast`が2倍の速度で移動するのでサイクルを必ず1周以上すること、`slow`は絶対に1周しないことが分かると、理解が進みました。 |
There was a problem hiding this comment.
There was a problem hiding this comment.
レビューありがとうございます。
科学手品的な手法にとらわれないように心がけます。
| func detectCycle(head *ListNode) *ListNode { | ||
| fast := head | ||
| slow := head | ||
|
|
||
| for fast != nil && fast.Next != nil { | ||
| fast = fast.Next.Next | ||
| slow = slow.Next | ||
|
|
||
| if fast == slow { | ||
| slow = head | ||
| for fast != slow { | ||
| fast = fast.Next | ||
| slow = slow.Next | ||
| } | ||
| return fast | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } |
There was a problem hiding this comment.
for-if-forとネストが深く、読みにくい印象を抱きます。
外側のforと内側のforは逐次実行できるので、分離してネストを浅くしたいですね。
例えば以下のように前半を関数化するのはどうでしょうか。
func hasCycle(head *ListNode) (bool, *ListNode) {
fast := head
slow := head
for fast != nil && fast.Next != nil {
fast = fast.Next.Next
slow = slow.Next
if fast == slow {
return true, fast
}
}
return false, nil
}
func detectCycle(head *ListNode) *ListNode {
found, meetingNode := hasCycle(head)
if !found {
return nil
}
fromHead := head
fromMeeting := meetingNode
for fromHead != fromMeeting {
fromHead = fromHead.Next
fromMeeting = fromMeeting.Next
}
return fromHead
}また、他にも書きかえ方はいくつかあり、oda-sanがまとめられた「コードの整え方」が大変参考になります。
There was a problem hiding this comment.
レビューありがとうございます。
迷いましたが、やはりネストは低く保つべきでした。
ご提案頂いた関数化案の方が、より意味が分かりやすいと思いました。
また、資料をご共有いただきありがとうございます。
参考にさせていただきます。
| } | ||
|
|
||
| nodes[head] = true | ||
| head = head.Next |
There was a problem hiding this comment.
head はリンクリストの先頭のノードを表す単語のため、 head が動いていくのに違和感を感じました。 node はいかがでしょうか?
There was a problem hiding this comment.
レビューありがとうございます。
headが「その時点の先頭」と勝手に自己解釈しておりました。
命名時にその意味をなるべくそのまま解釈して違和感がないように心がけます。
| ```Go | ||
| func detectCycle(head *ListNode) *ListNode { | ||
| visited := make(map[*ListNode]bool) | ||
| current := head |
There was a problem hiding this comment.
current という変数名にはあまり情報がないように思います。自分なら node と付けると思います。
previous/next と対比したいときには current と付けるのもありだと思います。
There was a problem hiding this comment.
リンクリストを、単一のnodeで表現してよいか悩んでおりました。
こちらシンプルにnodeにいたします。
This : 142. Linked List Cycle II
Next : 83. Remove Duplicates from Sorted List