Skip to content

Commit 61a30f4

Browse files
authored
Merge pull request #14 from colorbox/141
141. Linked List Cycle
2 parents dbf6eef + c2d27c5 commit 61a30f4

File tree

6 files changed

+120
-0
lines changed

6 files changed

+120
-0
lines changed

141/step1.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// 早いのと遅いので競争をするというネタを知っていたので、それをそのまま実装。
2+
class Solution {
3+
public:
4+
bool hasCycle(ListNode *head) {
5+
ListNode* fast = head;
6+
ListNode* slow = head;
7+
8+
while (fast && slow) {
9+
fast = fast->next;
10+
if (!fast) {
11+
return false;
12+
}
13+
fast = fast->next;
14+
15+
slow = slow->next;
16+
17+
if (slow == fast) {
18+
return true;
19+
}
20+
}
21+
22+
return false;
23+
}
24+
};

141/step2.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// whileの中をなるべくシンプルにするため、fast returnやfastの初期化部分を修正
2+
// while中のfastの状態チェックをwhileの継続条件に取り込む
3+
class Solution {
4+
public:
5+
bool hasCycle(ListNode *head) {
6+
if (!head) {
7+
return false;
8+
}
9+
10+
ListNode* fast = head->next;
11+
ListNode* slow = head;
12+
13+
while (fast && fast->next) {
14+
if (slow == fast) {
15+
return true;
16+
}
17+
18+
fast = fast->next->next;
19+
slow = slow->next;
20+
}
21+
22+
return false;
23+
}
24+
};

141/step3.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public:
3+
bool hasCycle(ListNode *head) {
4+
if (!head) {
5+
return false;
6+
}
7+
8+
auto fast = head->next;
9+
auto slow = head;
10+
11+
while (fast && fast->next) {
12+
if (fast == slow) {
13+
return true;
14+
}
15+
16+
fast = fast->next->next;
17+
slow = slow->next;
18+
}
19+
20+
return false;
21+
}
22+
};

141/step4.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// 他の人のコードを下に更に修正したもの
2+
// whileブロックの最初に`fast == slow`を持ってきていたために、fast/slowの初期化を変えたり、headのnullチェックをしていたが、fast==slowのチェック位置を変えるだけでそれらの処理をなくせた
3+
class Solution {
4+
public:
5+
bool hasCycle(ListNode *head) {
6+
auto fast = head;
7+
auto slow = head;
8+
9+
while (fast && fast->next) {
10+
fast = fast->next->next;
11+
slow = slow->next;
12+
13+
if (fast == slow) {
14+
return true;
15+
}
16+
}
17+
18+
return false;
19+
}
20+
};

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.contains(current)) {
9+
return true;
10+
}
11+
reached.insert(current);
12+
current = current->next;
13+
}
14+
return false;
15+
}
16+
};

141/with_set_2.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// insertの返り値を利用して存在確認をする方法
2+
class Solution {
3+
public:
4+
bool hasCycle(ListNode *head) {
5+
set<ListNode*> reached;
6+
ListNode* current = head;
7+
while (current) {
8+
auto [it, inserted] = reached.insert(current);
9+
if (!inserted) { return true; }
10+
current = current->next;
11+
}
12+
return false;
13+
}
14+
};

0 commit comments

Comments
 (0)