Linked List Cycle

Code:

class Solution {
public:
    bool hasCycle(ListNode *head) {
        ListNode *p=head;
        ListNode *cur=head;
        while(p){
            if(p->next)
                p=p->next->next;
            else
                return false;
            cur=cur->next;
            if(p==cur)
                return true;
        }
        return false;
    }
};

 

posted @ 2013-10-30 07:46  WinsCoder  阅读(100)  评论(0编辑  收藏  举报