-
Notifications
You must be signed in to change notification settings - Fork 0
Linked List Cycle 1 #2
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
base: main
Are you sure you want to change the base?
Conversation
test.txt
Outdated
@@ -0,0 +1,2 @@ | |||
test |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
問題と関係ないですが、こちらのファイルをPRから除けますか?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
申し訳ないです。
調べたのですがプルリクからの抜き方がよくわからなくてそのままにしております。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ファイルを削除して、コミットしたら消えませんか?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
コミットさせていただいたのですが、ファイル自体は削除されましたがこのコミット履歴はPRからは消えないようです。
申し訳ありません。
fast = fast.next.next | ||
if fast == slow: | ||
return True | ||
break |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
step2 で既に消されていますが、 return True
で関数から抜けるため、直後の break は余分だと思います。
その後動画を参考にコードを書いた。 | ||
|
||
```python | ||
class Solution: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
このアルゴリズムは、フロイドの循環検出法と呼ばれているアルゴリズムで、ソフトウェアエンジニアの常識には含まれていないと思います。想定解法は、辿ったノードを set 等に入れておき、過去に辿ったノードにたどり着いたら return True
するというものだと思います。そちらの解法でも書いてみていただけますか?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
わかりました。そちらの解法でも行ってみようと思います。
def hasCycle(self, head: Optional[ListNode]) -> bool: | ||
if not head: | ||
return False | ||
slow = head |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
slow = fast = head
とも確か書けましたね。それについての議論がdiscordにあった気がするので調べてみると良いかもです
|
||
0番目のリストを想定するのは無意味な仮定に思えるのでここでは採用しない。 | ||
"slowとfastが同じでない時、動かし続ける"と"fastとfast.nextが存在する時、動かし続ける"ではコードの意味が変わる気がする。 | ||
前者はループを見つけることに重きをおいていて後者はループがないものを見つけることに重きをおいていると言えるのではないか? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
すいません、ここに書かれていることをちゃんと理解したわけではないので、見当違いなことを言ってるかもしれないですが、ここでやっているフロイドの循環検出法は単純に、足の遅いAさんと足の速いBさんがいたとき、循環しているのであれば、いずれBさんはAさんに追いつくはずという理屈なので、難しく考えすぎている気もしました
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
なるほど、確かに難しく考えすぎていたかもしれません。
循環しているなら追いつくという理屈ならスタート地点はどこにしても結果は変わらないですね。
ありがとうございます。
```python | ||
class Solution: | ||
def hasCycle(self, head: Optional[ListNode]) -> bool: | ||
if not head: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pep8のこちらの記載を考慮すると、if head is None:
の方がおすすめです。
None のようなシングルトンと比較をする場合は、常に is か is not を使うべきです。絶対に等値演算子を使わないでください。また、 本当は if x is not None と書いているつもりで、 if x と書いている場合は注意してください - たとえば、デフォルトの値がNoneになる変数や引数に、何かしら別の値が設定されているかどうかをテストする場合です。この「別の値」は、ブール型のコンテクストでは False と評価される(コンテナのような)型かもしれませんよ!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
参照してみます。ご教授いただきありがとうございます。
何も見ないで三回連続で成功するまでコードを書いた | ||
一度`while fast or fast.next`としてしまった。コードの意味をきちんと理解していないから起こすミスであると考える。 | ||
|
||
最終的なコードはstep2と同様であるため示さない。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
あ、ちなみにこれ残しておいてもいいと思います。だんだんどう理解したのかが最後に現れるようになってきますから。
Linked List Cycle 1を行いました。Linked List Cycle 1 のコミットについてレビューお願いいたします
test pull requestは無視してください。プルリクの練習で行ったものです。
問題リンク
https://leetcode.com/problems/linked-list-cycle/submissions/1385160701/?source=submission-noac