141.Linked List Cycle

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;
    }
};
posted @ 2019-04-10 10:28  JohnRed  阅读(65)  评论(0编辑  收藏  举报