Skip to content

Commit 6992348

Browse files
committed
Add code with set
1 parent 16df05f commit 6992348

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

141/with_set.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// チェック済みのノードを記憶しておく解法のコード
2+
class Solution {
3+
public:
4+
bool hasCycle(ListNode *head) {
5+
set<ListNode*> reached;
6+
ListNode* current = head;
7+
while (current) {
8+
if (reached.count(current)) {
9+
return true;
10+
}
11+
reached.insert(current);
12+
current = current->next;
13+
}
14+
return false;
15+
}
16+
};

0 commit comments

Comments
 (0)