141. linked list cycle#1
Open
ntanaka1984 wants to merge 2 commits into
Open
Conversation
141. Linked List Cycle
add new directory
nodchip
reviewed
Jul 2, 2025
| class Solution { | ||
| public: | ||
| bool hasCycle(ListNode *head) { | ||
| std::set<ListNode*> visited_nodes ; |
There was a problem hiding this comment.
visited_nodes と ; の間のスペースは消すことをお勧めいたします。
Owner
Author
There was a problem hiding this comment.
| bool hasCycle(ListNode *head) { | ||
| std::set<ListNode*> visited_nodes ; | ||
|
|
||
| while (head != NULL) { |
Owner
Author
There was a problem hiding this comment.
ご指摘ありがとうございます。
https://cpprefjp.github.io/lang/cpp11/nullptr.html
NULL が単に整数0を表すマクロだとは知りませんでした。
以降気をつけます!
| std::set<ListNode*> visited_nodes ; | ||
|
|
||
| while (head != NULL) { | ||
| std::set<ListNode*>::iterator is_visited_it = visited_nodes.find(head) ; |
There was a problem hiding this comment.
C++11 以降では contains() を使用することをお勧めいたします。
if (visited_nodes.contains(head)) {
return true;
}
Owner
Author
There was a problem hiding this comment.
ご指摘ありがとうございます。
ここではhead がvisited_nodes に含まれているか否かだけに関心があるのでcontains という名前のメソッドのほうが意図が伝わりますね。
以降はcontains を使いたいと思います。
https://cpprefjp.github.io/reference/set/set/contains.html
| return true; | ||
| } | ||
| visited_nodes.insert(head) ; | ||
| head = head -> next ; |
Owner
Author
There was a problem hiding this comment.
ご指摘ありがとうございます!
そういえばメンバにアクセスする際の'.' でスペースあけるのは明らかに不自然なのですから、'->' でも空けないのが自然に感じられますね。以降気をつけます。
huyfififi
approved these changes
Jul 4, 2025
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/description/
Step3 まで完了しました。レビューのほどよろしくお願いいたします。