力扣:142. 环形链表 II

142. 环形链表 II
在这里插入图片描述

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode *detectCycle(struct ListNode *head) 
{
    
    
    struct ListNode* fast=head;
    struct ListNode* slow=head;
    while(fast!=NULL && fast->next!=NULL)
    {
    
    
        fast=fast->next->next;
        slow=slow->next;
        if(fast==slow)
        {
    
    
            struct ListNode* indx1=fast;
            struct ListNode* indx2=head;
            while(indx1!=indx2)
            {
    
    
                indx1=indx1->next;
                indx2=indx2->next;
            }
            return indx1;
        }
    }    
    return NULL;
}

猜你喜欢

转载自blog.csdn.net/congfen214/article/details/129567612
今日推荐