We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
2 parents 6935b73 + 72a6b4e commit e9a8832Copy full SHA for e9a8832
算法思维系列/双指针技巧.md
@@ -237,6 +237,28 @@ void reverse(int[] nums) {
237
238
======其他语言代码======
239
240
+[deardeer7](https://github.com/DearDeer7/) 提供 C++ 代码
241
+```cpp
242
+class Solution {
243
+public:
244
+ bool hasCycle(ListNode *head) {
245
+ // 链表为空或有一个元素,则无环
246
+ if(!head || !head->next) return false;
247
+
248
+ ListNode* slow = head;
249
+ ListNode* fast = head->next;
250
251
+ while(fast && fast->next) {
252
+ fast = fast->next->next;
253
+ slow = slow->next;
254
+ // 快慢指针相遇,则有环
255
+ if(fast == slow) return true;
256
+ }
257
+ return false; // 链表走完,快慢指针未相遇,则无环
258
259
+};
260
+```
261
262
[ryandeng32](https://github.com/ryandeng32/) 提供 Python 代码
263
```python
264
class Solution:
0 commit comments