Skip to content

142 linked list cycle ii#3

Open
MA-yo-TA wants to merge 3 commits into
mainfrom
142-linked-list-cycle-ii
Open

142 linked list cycle ii#3
MA-yo-TA wants to merge 3 commits into
mainfrom
142-linked-list-cycle-ii

Conversation

@MA-yo-TA

@MA-yo-TA MA-yo-TA commented Jun 2, 2026

Copy link
Copy Markdown
Owner

@MA-yo-TA MA-yo-TA self-assigned this Jun 2, 2026
set で書いた方は条件の入れ替えとかがあり得そうだけど自分の好み的には step1で書いたやつなので特に整形せず。
あとは while ループの中でまず if を書くか、一歩進めてから if を書くか、はどうだろう。`current_node` がループ入っていきなり次に進むよりは、「今のノードは条件満たすかな?」→「満たさないから次に行こう」の方が読みやすいかも?ぱっと見そういう人が多そう。

floyd の方は、fast と slow が出会う点を見つける処理を関数化するのが良いか。fast と slow を使い回すのはちょっと見づらい気がするので。あとは、最後の `while 1` だが、「合流するまで進みつづける。合流したらそこが答え」というのを素直に表現すると step2_floyd.py の最後の部分の方が好きかも。

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.

ありがとうございます。引き出しを増やして、意図を持って使い分けられるように練習していきます。

# fast と slow が合流したなら、片方をスタート地点に戻して同じ速さで再度歩かせると次に合流する点がサイクルの始まりの点
slow = head

while 1:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

あとは、最後の while 1 だが、「合流するまで進みつづける。合流したらそこが答え」というのを素直に表現すると step2_floyd.py の最後の部分の方が好きかも。

自分もstep2のほうが意図が伝わりやすく読みやすいと思いました。

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.

ありがとうございます!他の方の意見が聞けて助かります

# fast と slow が合流したなら、片方をスタート地点に戻して同じ速さで再度歩かせると次に合流する点がサイクルの始まりの点
slow = head

while 1:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

while True:

と書くことが多いと思います。

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.

ありがとうございます。他の方見てもそのようでしたので覚えておきます。



class Solution:
def detectMeetingPoint(self, head: Optional[ListNode]) -> Optional[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.

関数の名前は、snake_case で命名することをお勧めします。

参考までに、関連するスタイルガイドを共有いたします。

https://peps.python.org/pep-0008/#function-and-variable-names

Function names should be lowercase, with words separated by underscores as necessary to improve readability.

https://google.github.io/styleguide/pyguide.html#316-naming

function_name

LeetCode の解答では関数名が lowerCamel(mixedCase)で書かれていることがありますが、一般的な Python コードではあまり見かけません。PEP 8 でも以下のように、特殊な場合を除いて推奨されていません。

https://peps.python.org/pep-0008/#function-and-variable-names

mixedCase is allowed only in contexts where that’s already the prevailing style (e.g. threading.py), to retain backwards compatibility.

なお、これらのスタイルガイドが唯一の正解というわけではなく、数あるガイドラインの一つに過ぎません。
チームごとに好まれる命名規則が異なる場合もありますので、ご自身の中に基準を持ちつつ、最終的にはチームの一般的な書き方に合わせることをお勧めします。

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.

丁寧に教えていただきありがとうございます。lower camel case は見慣れないけどあり得るのかなと思っていましたが LeetCode がやや独特という感じなんですね。

なお、これらのスタイルガイドが唯一の正解というわけではなく、数あるガイドラインの一つに過ぎません。
チームごとに好まれる命名規則が異なる場合もありますので、ご自身の中に基準を持ちつつ、最終的にはチームの一般的な書き方に合わせることをお勧めします。

このスタンスを大事にします。

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.

4 participants