mirror of
https://github.com/chefyuan/algorithm-base.git
synced 2025-08-18 20:01:58 +00:00
链表专题更新cpp代码
This commit is contained in:
@@ -72,3 +72,24 @@ var hasCycle = function(head) {
|
||||
return false;
|
||||
};
|
||||
```
|
||||
|
||||
C++ Code:
|
||||
|
||||
```cpp
|
||||
class Solution {
|
||||
public:
|
||||
bool hasCycle(ListNode *head) {
|
||||
ListNode * fast = head;
|
||||
ListNode * low = head;
|
||||
while (fast != nullptr && fast->next != nullptr) {
|
||||
fast = fast->next->next;
|
||||
low = low->next;
|
||||
if (fast == low) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
|
Reference in New Issue
Block a user