【leetcode】环形链表

 

/*赋值解法*/
bool hasCycle(struct ListNode *head) { 
    while(head)
    {
        if (head->val == 1000000) return true;
        head->val=1000000;
        head=head->next;
    }
    return false;
}

 

 //使用快慢指针法
bool hasCycle(struct ListNode *head) 
{
    struct ListNode *fast=head,*slow=head;
    while((fast)!=NULL && (fast->next)!=NULL)
    {
        fast=fast->next->next;
        slow=slow->next;
        if(fast==slow)
        {
            return true;
        }
    }
    return false;
}

 

posted @ 2020-09-22 18:56  温暖了寂寞  阅读(130)  评论(0编辑  收藏  举报