Skip to content
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

Create 142LinkedListCicleII.md #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

SanakoMeine
Copy link
Owner

142.Linked List Cycleを解いた

142.Linked List Cycleを解いた
Comment on lines +28 to +30
else:
visited_node.add(node)
node = node.next
Copy link

@frinfo702 frinfo702 Dec 26, 2024

Choose a reason for hiding this comment

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

elseで書かなくてもいいのではないでしょうか?

Copy link
Owner Author

Choose a reason for hiding this comment

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

実際step2を経てそちらの方がなんとなく自然だなと感じるようになりましたが、やはりそちらの方が一般的な感覚なんだなと確信が持てました。ご指摘ありがとうございます。

node = head
visited_node = set()

while node and node.next is not None:
Copy link

Choose a reason for hiding this comment

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

Noneとの比較に、is notを使うものと使わないものが混在しているのが気になりました。

また、ここの部分でもwhile nodeだけでも正しく動くと思います。

class Solution:
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
node = head
visited_node = set()
Copy link

Choose a reason for hiding this comment

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

自分ならvisitedもしくはvisited_nodesとするかなと思います。

visited_node = set()
node = head

while node and node.next is not None:
Copy link

Choose a reason for hiding this comment

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

ここの部分はwhile nodeだけでも正しく動くと思います。while node and node.next is not None:としても、ループが一回回る回数が減るだけなので、自分ならwhile nodeにするかなと思います。

Copy link
Owner Author

Choose a reason for hiding this comment

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

深く考えずに過去のPRを反映していたのに気づけました。ご指摘ありがとうございます。

- https://github.com/ichika0615/arai60/pull/2/files

## Step 1
- ループが閉じる番号を探すなら順序を考慮するリストの方が良いのかなと思い、最初はリストで解いてみようとするも、計算時間オーバーでギブアップ
Copy link

Choose a reason for hiding this comment

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

in の判定が、中身を頭から全部舐めて確認しているので、数がたくさんに増えるにつれて遅くなっていくんですね。

Copy link
Owner Author

Choose a reason for hiding this comment

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

なるほど。「なんとなくこの辺のせいなのかな」くらいの感度しかなかったので、もう一度見返してみます。

## Step 1
- ループが閉じる番号を探すなら順序を考慮するリストの方が良いのかなと思い、最初はリストで解いてみようとするも、計算時間オーバーでギブアップ
- https://github.com/pineappleYogurt/leetCode/pull/3/files を拝見して、先の問題と同じようにsetで解いてnodeをreturnすれば良さそうと理解して書き直す
- すでに解いた方々の回答と付き合わせてもそんなに悪くなさそう…?今の実力だとどんなコメントがつくか予想がつかない。
Copy link

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/1246383603122966570/1252209488815984710
- 自分では絶対に思いつかないが、お絵描きしてみると言っていることは理解できた。時間が余った時にパズル的に聞かれることはあるとのことなので、おまけ気分で試しに書いてみる
- 先人のコードとレビューを見ていると、フロイドのアルゴリズムを使った場合はどうも前の問題でも言及されていたwhileの見通しが問題が生じやすそう:https://discord.com/channels/1084280443945353267/1221030192609493053/1225674901445283860 Python独自の書き方というのがよく分かっていないので、1度試しに書いてみる(Whileとelseを並列させている部分がPython独自なのかな?)
- が、たいしてif文の中が長くないので他の書き方でもよかった気がする。whileを2つ並べているのがまとめられそうでなんとなく気に食わない。

Choose a reason for hiding this comment

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

確かにfast_nodeとslow_nodeがぶつかった場合に片方のポインタを最初の位置に戻してまた一つずつ動かせということなので、二つあるwhile文を一つにまとめることはできると思います。while文の中が長くなっちゃうので読めるようにする工夫はいるかもしれません。

```Python
class Solution:
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
fast_node = head

Choose a reason for hiding this comment

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

この関数内で扱われる対象がNodeであることは明らかであるため、わざわざnodeまで書かなくてもいいのではと思いましたがいかがでしょう?
katataku/leetcode#2 (comment)

Copy link
Owner Author

Choose a reason for hiding this comment

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

なるほど、ありがとうございます

else:
return None

node_from_start = head

Choose a reason for hiding this comment

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

上同様ここもnodeであることは明らかで、使用する範囲も数行なのでstartなどでもいいかなと思いました(startだとちょっと汎用的すぎるかもですが)

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.

6 participants