Skip to content

Solve 142. Linked List Cycle II (step 1, 2)#7

Open
cheeseNA wants to merge 2 commits intomainfrom
142
Open

Solve 142. Linked List Cycle II (step 1, 2)#7
cheeseNA wants to merge 2 commits intomainfrom
142

Conversation

@cheeseNA
Copy link
Copy Markdown
Owner

Problem link
参照した他の解答についてはnote.mdに記載しております.

Copy link
Copy Markdown

@nodchip nodchip left a comment

Choose a reason for hiding this comment

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

気になるところは特にありませんでした。良いと思います。

Copy link
Copy Markdown

@hayashi-ay hayashi-ay left a comment

Choose a reason for hiding this comment

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

良いと思います。

# step1

set を使用した space complexity O(N)の方法はすぐに思いつく.
問題は O(1)の場合. Floyd のアルゴリズムで, 合流までの回数などを用いて計算すれば求まりそうだが, あまり自明ではないだろう. Brent のアルゴリズムを使えば求められるだろうが, これも常識の範囲には入らず, 読むには負荷がかかるのではないだろうか.
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Brent’s Cycle Detection Algorithmは自分は知らなかったです。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

class Solution:
    def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
        def find_cycle_length(start_node):
            power = 1
            length = 0
            slow = start_node
            fast = start_node
            while fast:
                fast = fast.next
                length += 1
                if slow == fast:
                    return length
                if length == power:
                    power *= 2
                    length = 0
                    slow = fast
            return -1
        
        def step_node_by_n(node, n):
            while n > 0:
                node = node.next
                n -= 1
            return node
        
        cycle_length = find_cycle_length(head)
        if cycle_length == -1:
            return None
        # 開始点と開始点からcycle_lengthだけ動いた位置から1つずつポインターを進めて合流地点がループの開始地点になる
        p1 = head
        p2 = step_node_by_n(head, cycle_length)
        while p1 != p2:
            p1 = p1.next
            p2 = p2.next
        return p1

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます!141. Linked List Cycleを解いていたときにこのwikiを見てたまたま知っていたのですが, 勝手にループの開始地点が分かると勘違いしていました. 実装参考になります, Floydのときと同じような形で開始点も見つけられそうなのですね.


- Google docs の sample answer
- https://discord.com/channels/1084280443945353267/1195700948786491403/1196010117120925777
- キャンベルの法則を知る
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

グッドハートの法則とかも近いですね。こういった法則は博物学的な感じで統一的に何かを説明するものではないですが、名前を知っていることで存在を意識できるようになりますね(ジョシュアツリーの法則)

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

なるほど, ”名前を知ると意識できるようになる”という法則にも名前がついているのですね.
ジョシュアツリーの法則で検索したら他にもいろいろな法則を知ることができました. (https://ktr-05.hatenablog.com/entry/2019/07/07/184436)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants