Skip to content

142. Linked List Cycle II#3

Open
hemispherium wants to merge 2 commits intomainfrom
0142-linked-list-cycle-2
Open

142. Linked List Cycle II#3
hemispherium wants to merge 2 commits intomainfrom
0142-linked-list-cycle-2

Conversation

@hemispherium
Copy link
Owner

@hemispherium hemispherium self-assigned this Dec 10, 2025
}
return nullptr;
}
}; No newline at end of file

Choose a reason for hiding this comment

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

ファイルの末尾は改行を入れることをお勧めします

set<ListNode*> used;
ListNode* curr = head;
while (curr != nullptr) {
if (used.count(curr)) return curr;

Choose a reason for hiding this comment

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

ifブロックの終わりがどこか読み辛くなるので、ブロックを用いる方が好みです。
Googleのスタイルガイドには以下のような記載もございます。
Put any controlled statements inside blocks (i.e., use curly braces).
https://google.github.io/styleguide/cppguide.html#Formatting_Looping_Branching

Copy link
Owner Author

Choose a reason for hiding this comment

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

コメントありがとうございます!ブロックを使った書き方をするようにします。

Choose a reason for hiding this comment

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

Googleガイドも一例ですので、ここの会に参加されてる他の皆さんのカードを見るなり、議論を見るなりして好みや考え方を身につけていけば良いのかなと思います。

set<ListNode*> used;
ListNode* curr = head;
while (curr != nullptr) {
if (used.count(curr)) return curr;

Choose a reason for hiding this comment

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

containsを使った方法もございます。
https://en.cppreference.com/w/cpp/container/set/contains.html

Copy link
Owner Author

Choose a reason for hiding this comment

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

containsは知らなかったです、ありがとうございます。

@@ -0,0 +1,11 @@
### step1

141. Linked List Cycleでsetによる解法でも解いてみていたので、そちらの解法と同じようにすると解けた。
Copy link

@Ryotaro25 Ryotaro25 Dec 10, 2025

Choose a reason for hiding this comment

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

時間計算量や空間計算量についての言及があればなおいいのかなと思いました。

Linked List Cycleでsetによる解法でも解いてみていた

レビューする人によっては上記を知らない可能性があるので説明があればより良いのかなと思いました。

Copy link
Owner Author

Choose a reason for hiding this comment

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

時間計算量や空間計算量についてはなんとなくしか理解できていないので、少し勉強してみます。

@@ -0,0 +1,11 @@
### step1

141. Linked List Cycleでsetによる解法でも解いてみていたので、そちらの解法と同じようにすると解けた。

Choose a reason for hiding this comment

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

Linked List Cycleでもあったフロイドの循環検出を使っても書けることは認識しておられると思うので、学習のために検討しても良いかなと思いました。

katsukii/leetcode#13 (comment)

この問題は、どちらかというと、このあたりのコードの整え方を見ておくといいかもしれません。

コードの整え方の練習にもなるので。

public:
ListNode *detectCycle(ListNode *head) {
set<ListNode*> used;
ListNode* curr = head;

Choose a reason for hiding this comment

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

好みの範疇かもしれませんが、currはpreviousなどと対比して使うイメージがあるので、node等の名前の方がいいのではないかと思いました。

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.

4 participants