struct ListNode* EntryNodeOfLoop(struct ListNode* pHead ) {
    // write code here
    if (pHead == NULL || pHead->next == NULL) {
        return NULL;
    }

    struct ListNode* slow = pHead->next;
    struct ListNode* fast = pHead->next->next;

    while (fast != NULL && fast->next != NULL) {
        if (slow == fast) {
            // 链表中存在环
            slow = pHead;
            while (slow != fast) {
                slow = slow->next;
                fast = fast->next;
            }
            return slow;
        }
        slow = slow->next;
        fast = fast->next->next;
    }

    return NULL; // 链表中不存在环
}
全部评论

相关推荐

zYvv:双一流加大加粗再标红,然后广投。主要是获奖荣誉不够,建议开始不用追求大厂,去别的厂子刷下实习。
点赞 评论 收藏
分享
自学java狠狠赚一...:骗你点star的,港卵公司,记得把star收回去
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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