Skip to content

141. Linked List Cycle#1

Open
tarinaihitori wants to merge 2 commits intomainfrom
141-linked-list-cycle
Open

141. Linked List Cycle#1
tarinaihitori wants to merge 2 commits intomainfrom
141-linked-list-cycle

Conversation

@tarinaihitori
Copy link
Copy Markdown
Owner

def hasCycle(self, head: Optional[ListNode]) -> bool:
visited = set()
current = head
while current:
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.

Google Style Guide はis not None派。

not None は二重否定っぽくてあまり好みではなかったのですが、Google Style Guide では is not None が推奨されているんですね。None と他の値(例えば 0 や空文字列)を区別するために、明示的に None との比較を行うことで意図が明確になるという点は納得できました。今後は is not None も意識して使ってみようと思います。

```python
class Solution:
def hasCycle(self, head: Optional[ListNode]) -> bool:
def _has_cycle(current, visited):
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.

ありがとうございます。関数を分けて実装するのは頭になかったです。
関数を分けて実装するメリットとして、再利用性が上がったり、テストのしやすさ、読みやすさなどがありそうですね。
逆にinner functionとすることで、_has_cycleの範囲が限定されて管理しやすいというメリットもあると思います。
選択肢をもつ意味でも、step4では関数を分けて書いてみます。

@tarinaihitori tarinaihitori changed the title step3まで 141. Linked List Cycle Oct 17, 2024
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.

3 participants