-
Notifications
You must be signed in to change notification settings - Fork 0
142. Linked List Cycle #3
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,73 @@ | ||
"""効率的な方法が特に思いつかなかったので愚直にやろうと思った。また、ListNodeの扱い方がよくわからなかったのでpythonのリストを用いた。一見良さそうなコードが書けたが、Nodeの中に同じ数字が入っているとダメであることに気づかずそこでsubmitも止まってしまった。(15分)""" | ||
|
||
class Solution: | ||
def hasCycle(self, head: Optional[ListNode]) -> bool: | ||
head_list = [] | ||
while head: | ||
if head.val in head_list: | ||
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以降はset()になっているので問題ないですが、この問題ではlistを使うかsetを使うかで顕著にスピード差が出ます。 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. 探索をするときの時間計算量の話で、setはhashで探索を、listは前から順に探索をしているからだと理解しています。ただ実は、step1~step3をやっている間はこれについて意識していなかったです。他の方のレビューを見てから調べたりして把握しました。 |
||
return True | ||
head_list.append(head.val) | ||
head = head.next | ||
|
||
return False | ||
|
||
"""step2をやった。step1で私が書いたコードと非常に似ていると感じた。違いはset()を使用していること、headをそのまま使用しないで他の変数に置いているというところか。後の違いは私のコードはvalで比較をしているのに対して解答はノードで比較している点だ。この場合はvalと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. setの要素にできるか否かは、pythonの場合はその要素がhashableであるかどうかによります。 |
||
|
||
別解のフロイドの循環検出アルゴリズムは、今の段階で理解できていない。なんで、循環している時はfast.next.nextとslow.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の二人がN歩で一周できる池の周りをぐるぐる周るとします。 一方で、循環がない場合はBさんが先に家に帰っちゃう(fast is Noneになる)ので二人が会うことはないです。 |
||
|
||
class Solution: | ||
def hasCycle(self, head: Optional[ListNode]) -> bool: | ||
visited_node = set() | ||
current_node = head | ||
|
||
while current_node: | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more. 変数に関してですね、参考にさせていただきます。 |
||
if current_node in visited_node: | ||
return True | ||
visited_node.add(current_node) | ||
current_node = current_node.next | ||
|
||
return False | ||
|
||
class Solution: | ||
def hasCycle(self, head: Optional[ListNode]) -> bool: | ||
fast = head | ||
slow = head | ||
|
||
while fast and fast.next: | ||
fast = fast.next.next | ||
slow = slow.next | ||
|
||
if fast == slow: | ||
return True | ||
|
||
return False | ||
|
||
"""step3 いいのかはわからないけど、ほとんど暗記されていたので1分で書き終わりました。別解も1分程度で書き終わりました。""" | ||
|
||
class Solution: | ||
def hasCycle(self, head: Optional[ListNode]) -> bool: | ||
current_node = head | ||
visited_node = set() | ||
Comment on lines
+49
to
+50
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. node, visitedでも十分かもしれません。 |
||
|
||
while current_node: | ||
if current_node in visited_node: | ||
return True | ||
|
||
visited_node.add(current_node) | ||
current_node = current_node.next | ||
|
||
return False | ||
|
||
class Solution: | ||
def hasCycle(self, head: Optional[ListNode]) -> bool: | ||
fast = head | ||
slow = head | ||
|
||
while fast and fast.next: | ||
fast = fast.next.next | ||
slow = slow.next | ||
|
||
if fast == slow: | ||
return True | ||
|
||
return 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.
計算量の見積もり(時間、空間)もやっておくといいかもです。
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.
計算量の見積もりは、計算時間を見積もるためのものなので、本当は時間まで見積もっておいたほうがいいです。ただ、そんなに高い精度は必要ではありません。