#Leetcode# 141. Linked List Cycle

https://leetcode.com/problems/linked-list-cycle/

Given a linked list, determine if it has a cycle in it.

Follow up:
Can you solve it without using extra space?

Accepted
324,757
Submissions
938,662
Seen this question in a real interview before?

代码:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
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;
    }
};

  快慢指针 如果是个环的话那么快慢指针一定会遇到! 第一次遇到快慢指针的问题居然是在链表里 毕竟链表我还没看很懂啊

原文地址:https://www.cnblogs.com/zlrrrr/p/10042648.html