diff --git "a/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\345\217\214\346\214\207\351\222\210\346\212\200\345\267\247.md" "b/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\345\217\214\346\214\207\351\222\210\346\212\200\345\267\247.md" index 11f1b56ab80c1b82eca0fea335c96cc198d04ec9..eab83406d0120b49ece8a083bcb3f3ca1a0fa984 100644 --- "a/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\345\217\214\346\214\207\351\222\210\346\212\200\345\267\247.md" +++ "b/\347\256\227\346\263\225\346\200\235\347\273\264\347\263\273\345\210\227/\345\217\214\346\214\207\351\222\210\346\212\200\345\267\247.md" @@ -237,6 +237,28 @@ void reverse(int[] nums) { ======其他语言代码====== +[deardeer7](https://github.com/DearDeer7/) 提供 C++ 代码 +```cpp +class Solution { +public: + bool hasCycle(ListNode *head) { + // 链表为空或有一个元素,则无环 + if(!head || !head->next) return false; + + ListNode* slow = head; + ListNode* fast = head->next; + + while(fast && fast->next) { + fast = fast->next->next; + slow = slow->next; + // 快慢指针相遇,则有环 + if(fast == slow) return true; + } + return false; // 链表走完,快慢指针未相遇,则无环 + } +}; +``` + [ryandeng32](https://github.com/ryandeng32/) 提供 Python 代码 ```python class Solution: