1 Star 0 Fork 0

李勇震/c语言每日练习

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
test_3_17(环形链表,以及求节点) 1.35 KB
一键复制 编辑 原始数据 按行查看 历史
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;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/liyongzhen2004/2022.10.24.git
git@gitee.com:liyongzhen2004/2022.10.24.git
liyongzhen2004
2022.10.24
c语言每日练习
master

搜索帮助