代码拉取完成,页面将自动刷新
struct ListNode *detectCycle(struct ListNode *head) {
struct ListNode* fast = head;
struct ListNode* slow = head;
while(fast && fast->next)
{
slow = slow->next;
fast = fast->next->next;
if(fast == slow)
{
struct ListNode* meetNode = fast;
struct ListNode* list = meetNode->next;
int lenA = 1;
struct ListNode* head1 = head;
struct ListNode* head2 = list;
while(head1 != meetNode)
{
head1 = head1->next;
lenA++;
}
int lenB = 1;
while(head2 != meetNode)
{
head2 = head2->next;
lenB++;
}
int gap = abs(lenA-lenB);
struct ListNode* longList = head;
struct ListNode* shortList = list;
if(lenA < lenB)
{
longList = list;
shortList = head;
}
while(gap--)
{
longList = longList->next;
}
while(longList != shortList)
{
longList = longList->next;
shortList = shortList->next;
}
return longList;
}
}
return NULL;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。