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;
}

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

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

posted @ 2018-12-01 11:20  xinfenglee  阅读(94)  评论(0编辑  收藏  举报