We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
contain
insert
1 parent 6992348 commit c2d27c5Copy full SHA for c2d27c5
141/with_set.cpp
@@ -5,7 +5,7 @@ class Solution {
5
set<ListNode*> reached;
6
ListNode* current = head;
7
while (current) {
8
- if (reached.count(current)) {
+ if (reached.contains(current)) {
9
return true;
10
}
11
reached.insert(current);
141/with_set_2.cpp
@@ -0,0 +1,14 @@
1
+// insertの返り値を利用して存在確認をする方法
2
+class Solution {
3
+public:
4
+ bool hasCycle(ListNode *head) {
+ set<ListNode*> reached;
+ ListNode* current = head;
+ while (current) {
+ auto [it, inserted] = reached.insert(current);
+ if (!inserted) { return true; }
+ current = current->next;
+ }
12
+ return false;
13
14
+};
0 commit comments