Linked List Cycle

class Solution {
public:
    bool hasCycle(ListNode *head) {
        if(!head)
            return NULL;
        ListNode* fast=head;
        ListNode* slow=head;
        while(fast->next&&fast->next->next)
        {
            slow=slow->next;
            fast=fast->next->next;
            if(slow==fast)
                return true;
        }
        return false;
    }
};

 

posted on 2016-06-07 14:41  summerkiki  阅读(166)  评论(0编辑  收藏  举报