-
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# LinkedListCycle1 | ||
|
||
## step1 | ||
|
||
所要時間 30分 | ||
|
||
10分ほど考えてわからなかったので解説動画を見た。 | ||
Linked Listを知らなかったので調べた。 | ||
[Linked Listとは](https://astrostory.hatenablog.com/entry/2020/02/24/070213) | ||
[Linked ListとArray Listの違い](https://qiita.com/BumpeiShimada/items/522798a380dc26c50a50) | ||
その後動画を参考にコードを書いた。 | ||
|
||
```python | ||
class Solution: | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
fast = head | ||
while fast and fast.next: | ||
slow = slow.next | ||
fast = fast.next.next | ||
if fast == slow: | ||
return True | ||
break | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. step2 で既に消されていますが、 |
||
else: | ||
return False | ||
|
||
``` | ||
|
||
## step2 | ||
|
||
所要時間30分 | ||
|
||
step1を修正する | ||
returnを行うと関数自体が終了することを理解していなかったため必要のないbreakとelseを入れてしまった。 | ||
|
||
```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 commentThe reason will be displayed to describe this comment to others. Learn more. pep8のこちらの記載を考慮すると、
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 参照してみます。ご教授いただきありがとうございます。 |
||
return False | ||
slow = head | ||
fast = head | ||
while fast and fast.next: | ||
slow = slow.next | ||
fast = fast.next.next | ||
if fast == slow: | ||
return True | ||
#break 削除 | ||
#else: 削除 | ||
return False | ||
|
||
``` | ||
|
||
<https://qiita.com/toshi_machine/items/3b2a5c04da949ac78298> | ||
上のリンクで紹介されているコードがなぜうまくいくのかわからないので考えてみた。 | ||
|
||
```python | ||
class Solution: | ||
def hasCycle(self, head: ListNode) -> bool: | ||
if not head: | ||
return False | ||
slow = head | ||
fast = head.next | ||
#なぜfast = head.nextとしても通るのだろうか | ||
#0番目のリンクリストが存在するとして考えれば良い。またここをfast = head.nextとしないと次の slow != fastに引っかかる | ||
|
||
while slow != fast: | ||
if not fast or not fast.next: | ||
return False | ||
slow = slow.next | ||
fast = fast.next.next | ||
return True | ||
``` | ||
|
||
0番目のリストを想定するのは無意味な仮定に思えるのでここでは採用しない。 | ||
"slowとfastが同じでない時、動かし続ける"と"fastとfast.nextが存在する時、動かし続ける"ではコードの意味が変わる気がする。 | ||
前者はループを見つけることに重きをおいていて後者はループがないものを見つけることに重きをおいていると言えるのではないか? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. なるほど、確かに難しく考えすぎていたかもしれません。 |
||
|
||
## step3 | ||
|
||
所要時間5分 | ||
|
||
何も見ないで三回連続で成功するまでコードを書いた | ||
一度`while fast or fast.next`としてしまった。コードの意味をきちんと理解していないから起こすミスであると考える。 | ||
|
||
最終的なコードはstep2と同様であるため示さない。 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
このアルゴリズムは、フロイドの循環検出法と呼ばれているアルゴリズムで、ソフトウェアエンジニアの常識には含まれていないと思います。想定解法は、辿ったノードを 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.
わかりました。そちらの解法でも行ってみようと思います。