Skip to content

Commit 16df05f

Browse files
committed
Add step4
1 parent 3d4a0c6 commit 16df05f

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

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+
};

0 commit comments

Comments
 (0)