c_cpp 141.链表循环 - 简易 - 2018.7.30

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 141.链表循环 - 简易 - 2018.7.30相关的知识,希望对你有一定的参考价值。

/**
1:两个指针,slowPointer 每次步进 1,fastPointer 每次步进 2
2:如果快指针到尽头了,说明没有循环
3:如果两个指针最后碰头了,说明存在一个循环
 */
 
class Solution {
public:
    bool hasCycle(ListNode *head) {
        ListNode *slow = head, *fast = head;
        while (fast && fast->next) {
            slow = slow->next;
            fast = fast->next->next;
            if (slow == fast) return true;
        }
        return false;
    }
};

以上是关于c_cpp 141.链表循环 - 简易 - 2018.7.30的主要内容,如果未能解决你的问题,请参考以下文章