Open
Conversation
oda
reviewed
Oct 16, 2024
| def hasCycle(self, head: Optional[ListNode]) -> bool: | ||
| visited = set() | ||
| current = head | ||
| while current: |
There was a problem hiding this comment.
current is not Noneと書く方法もあります。これはよしあしあってどちらでもいいですかね。
Google Style Guide はis not None派。
https://google.github.io/styleguide/pyguide.html#2144-decision
Owner
Author
There was a problem hiding this comment.
Google Style Guide はis not None派。
not None は二重否定っぽくてあまり好みではなかったのですが、Google Style Guide では is not None が推奨されているんですね。None と他の値(例えば 0 や空文字列)を区別するために、明示的に None との比較を行うことで意図が明確になるという点は納得できました。今後は is not None も意識して使ってみようと思います。
rihib
reviewed
Oct 16, 2024
| ```python | ||
| class Solution: | ||
| def hasCycle(self, head: Optional[ListNode]) -> bool: | ||
| def _has_cycle(current, visited): |
Owner
Author
There was a problem hiding this comment.
ありがとうございます。関数を分けて実装するのは頭になかったです。
関数を分けて実装するメリットとして、再利用性が上がったり、テストのしやすさ、読みやすさなどがありそうですね。
逆にinner functionとすることで、_has_cycleの範囲が限定されて管理しやすいというメリットもあると思います。
選択肢をもつ意味でも、step4では関数を分けて書いてみます。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
https://leetcode.com/problems/linked-list-cycle/