Skip to content

142/Linked List Cycle II#4

Open
ask-1407 wants to merge 7 commits intomainfrom
linked_list_cycle_ii
Open

142/Linked List Cycle II#4
ask-1407 wants to merge 7 commits intomainfrom
linked_list_cycle_ii

Conversation

@ask-1407
Copy link
Copy Markdown
Owner

Comment on lines +85 to +90
if not head:
return Non
slow = head
fast = head

while fast and fast.next:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

slow と fast はその下の while 文から参照されるので、以下のようにした方が自然に感じました。

Suggested change
if not head:
return Non
slow = head
fast = head
while fast and fast.next:
if not head:
return Non
slow = head
fast = head
while fast and fast.next:

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.

whileにつなげることで処理の関係性を暗に示せると解釈しました。確かにそのほうがブロックとしてまとまっていて自然と感じました。


```python
class Solution:
def detectCycle(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.

再帰での書き換えはよく求められるそうなので、そちらでも書けるようにしておくといいと思います。

Comment on lines +24 to +25
if temp in visited:
return temp
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

疑問点:

  • setでもリストでもとおったが、どっちのほうがよいのだろうか?

まずハッシュセットとリストの違いを調べることをおすすめします。
もじ調べてみてピンとこなかったら、この部分の時間計算量を比べてみることを合わせておすすめします。

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.

ハッシュセットが重複なしで要素を持つのに対しリストは重複許容・順序をもつこと程度は理解していましたが、速度に違いがあることは調べて理解できました。

※以下、自分用

  • 要素の存在確認においてはハッシュセットのほうがリストのほうが早い。
  • リストは最悪全ての要素を確認する(O(n) となる)のに対し、ハッシュセットは要素のハッシュ値を計算して直接位置にアクセスするのでO(1)ですむ。

if not head:
return None

temp=head
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

個人的にはtempという変数名はスコープが狭くて一時保存であることを強調したいときのみにするのが良いと思います。
node とか current をよく見かけます。

Copy link
Copy Markdown
Owner Author

@ask-1407 ask-1407 Jul 1, 2024

Choose a reason for hiding this comment

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

current は他の方のPRで小田さんが違和感をあるとコメントしてくださっていたのを見て避けていました。(リンク)
おっしゃるとおりtempも命名的には微妙ですね、素直にnodeがいいかなと思いました。

return None

temp_node=head
visited=set() #setでもOKだったがどっちがよいのか?
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

setは検索が average O(1) で出来ますね。
https://docs.python.org/3/tutorial/datastructures.html#sets

Copy link
Copy Markdown
Owner Author

@ask-1407 ask-1407 Jul 1, 2024

Choose a reason for hiding this comment

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

ドキュメントリンク送付ありがとうございます。普段からここの参照を意識できるようにします。

```python
class Solution:
def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
if not head:
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.

あー、後続のwhileでカバーができてますね…

Comment on lines +100 to +101
fast = fast.next
slow = slow.next
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

この時点ではfastとslowはもはや同じスピードで動いているので変数名を変えてあげると良さそうです。

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.

実態と変数がずれていて違和感があると解釈しました。別変数で用意します。

Copy link
Copy Markdown

@tk-hirom tk-hirom left a comment

Choose a reason for hiding this comment

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

出遅れました!他の方が指摘してある箇所以外は良いかと思います

if not head:
return None

temp=head
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

= の両隣に空白を空けることをお勧めいたします。

https://peps.python.org/pep-0008/#other-recommendations

Always surround these binary operators with a single space on either side: assignment (=), augmented assignment (+=, -= etc.), comparisons (==, <, >, !=, <>, <=, >=, in, not in, is, is not), Booleans (and, or, not).

https://google.github.io/styleguide/pyguide.html#s3.6-whitespace

Surround binary operators with a single space on either side for assignment (=), comparisons (==, <, >, !=, <>, <=, >=, in, not in, is, is not), and Booleans (and, or, not). Use your better judgment for the insertion of spaces around arithmetic operators (+, -, *, /, //, %, **, @).


while temp_node:
if temp_node in visited:
visited.add(temp_node)
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.

おっしゃる通りずれてました、転記の際に少し手直ししたせいでした

return None
```

- フロイドの循環検出法を実装したがTLE。解法をみた(下記は通らなかった実装)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

フロイドの循環検出法は、なぜか知っている人が多いのですが、想定解や、期待されている解ではないと思います。 set を使ったほうが想定解だと思います。

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.

違う視点での解法を試す意図で、前回実装したこともありこちらを試していました。
setを使った解法が想定解だと思われる理由は何でしょうか?(個人的に、どちらがぱっと思いつくかといわれるとsetのほうな気はしています)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

setを使った解法が想定解だと思われる理由は何でしょうか?(個人的に、どちらがぱっと思いつくかといわれるとsetのほうな気はしています)

フロイドの循環検出法が、ソフトウェアエンジニアの常識に含まれないと感じるためです。

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.

ありがとうございます。正直自身に常識がインストールされていないせいか、まだ完全に線引きが言語化できていないですが一旦そのまま受け止めてみます。

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.

5 participants