Skip to content

Create 141. Linked List Cycle.md#37

Merged
Mike0121 merged 2 commits intomainfrom
141.-Linked-List-Cycle
Nov 1, 2025
Merged

Create 141. Linked List Cycle.md#37
Mike0121 merged 2 commits intomainfrom
141.-Linked-List-Cycle

Conversation

@Mike0121
Copy link
Copy Markdown
Owner

@Mike0121 Mike0121 commented Jul 6, 2024

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

全体としてよくかけていると思いました.

fast = slow = head

も選択肢でしょうか?

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.

ありがとうございます。
そうですね、個人的に少し1行に詰め込むと読みづらいと感じる時があるので、この書き方にしています。

Copy link
Copy Markdown

@NobukiFukui NobukiFukui Jul 6, 2024

Choose a reason for hiding this comment

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

様々な解法をご検討されており,よいと思います.
ほかにも,
・関数を用いる方法(以下の部分で実装されていますが,フロイドの循環検出法でも可能かと思いました)
・無限ループを用いる方法

class Solution:
    def hasCycle(self, head: Optional[ListNode]) -> bool:
        fast = slow = head
        while 1:
            if fast is None or fast.next is None:
                return None
            slow = slow.next
            fast = fast.next.next
            if slow == fast:
                return True

もいかがでしょうか

以下の議論も参考になります
https://discordapp.com/channels/1084280443945353267/1192728121644945439/1194203372115464272
https://discordapp.com/channels/1084280443945353267/1221030192609493053/1225674901445283860

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.

ありがとうございます。無限ループの書き方もわかりやすいですね。参考になります。
参考議論もありがとうございます!

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

if slow == fast:ではなくif slow is fast:とするのには何か理由があるのでしょうか。

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.

ListNode同士のオブジェクトの比較なので、isにしています。ここは同一のListNodeの比較想定なので、=でも問題ないですね。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

私の間違いだったら申し訳ないのですが,この下にseenにheadが含まれない場合はaddする必要がある気がします.

else:
    seen.add(head)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

私もadd必要だと思います。
ifの中でreturnしているので、ifの外であればelseはなくてもよさそうです。

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.

すみません、ご指摘頂きありがとうございます。
コピペの段階で誤って消したみたいです。クリティカルなのでここだけ部分的に修正しました。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

これはおそらくクラスメソッドではなくインスタンスメソッドの話かなと思いました。
(クラスメソッドの最初の引数は一般的にはselfではなくcls)

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.

ありがとうございます。ご指摘の通りインスタンスメソッドですね。

@Mike0121 Mike0121 changed the title Create 141. Linked List Cycle pr.md Create 141. Linked List Cycle.md Nov 1, 2025
@Mike0121 Mike0121 merged commit 9d6fabd into main Nov 1, 2025
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