首页 > 其他 > 详细

【leetcode】环形链表

时间:2020-09-22 19:28:34      阅读:48      评论:0      收藏:0      [点我收藏+]

 

/*赋值解法*/
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;
}

 

【leetcode】环形链表

原文:https://www.cnblogs.com/ganxiang/p/13714175.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!