题解 | #判断链表中是否有环#

判断链表中是否有环

https://www.nowcoder.com/practice/650474f313294468a4ded3ce0f7898b9

#include <stdbool.h>
#include <stdio.h>
/**
 * struct ListNode {
 *	int val;
 *	struct ListNode *next;
 * };
 */

/**
 * 
 * @param head ListNode类 
 * @return bool布尔型
 */
bool hasCycle(struct ListNode* head ) {
    // write code here
    struct ListNode* low = head;
    struct ListNode* fast = head;
    int q=0;
    while(fast != NULL && fast->next != NULL){
        
        if(low->next == fast->next->next){
            struct ListNode* head1 = head;
            struct ListNode* head2 =low->next;
            while(head1 != head2){
                head1 = head1->next;
                head2 = head2->next;
                q++;//计算环的入口
            }
            return true;
        }
        low = low->next;
        fast = fast->next->next;

    }
    return false;
}

全部评论

相关推荐

06-17 21:57
门头沟学院 Java
白友:噗嗤,我发现有些人事就爱发这些,明明已读不回就行了,就是要恶心人
点赞 评论 收藏
分享
机械打工仔:我来告诉你原因,是因为sobb有在线简历,有些HR为了快会直接先看在线简历,初步感觉不合适就不会找你要详细的了
投了多少份简历才上岸
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务