leetcode141-环形链表

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
bool hasCycle(struct ListNode *head) {
    struct ListNode *slow, *fast;
    slow = fast = head;
    while(slow != NULL && fast != NULL && fast->next != NULL)
    {
        slow = slow->next;
        fast = fast->next->next;
        if(slow == fast)
            return true;
    }
    return false;
}

给定一个链表,判断链表中是否有环。

进阶:
你能否不使用额外空间解决此题?

原文地址:https://www.cnblogs.com/xinfenglee/p/10048758.html