diff --git a/LeetCode-CN/141.cpp b/LeetCode-CN/141.cpp new file mode 100644 index 0000000..e6a0cb9 --- /dev/null +++ b/LeetCode-CN/141.cpp @@ -0,0 +1,14 @@ +class Solution { +public: + bool hasCycle(ListNode *head) { + if (head == nullptr || head->next == nullptr) return false; + ListNode* p = head; + ListNode* q = head->next->next; + while (p != q && q && q->next) + { + p = p->next; + q = q->next->next; + } + return (p == q); + } +};