Leetcode—142.环形链表II【中等】
2023每日刷题(三十三)
Leetcode—142.环形链表II
实现代码
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode *detectCycle(struct ListNode *head) {
struct ListNode* fast = head, *slow = head;
while(1) {
if(fast == NULL || fast->next == NULL) {
return NULL;
}
fast = fast->next->next;
slow = slow->next;
if(slow == fast) {
break;
}
}
fast = head;
while(slow != fast) {
fast = fast->next;
slow = slow->next;
}
return fast;
}
运行结果
之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!