AcWing 34. 双指针
原题链接
中等
作者:
史一帆
,
2021-04-28 21:45:30
,
所有人可见
,
阅读 289
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *entryNodeOfLoop(ListNode *head) {
if (!head || !head -> next) return 0;
ListNode *first = head, *second = head;
while (first && second)
{
first = first -> next;
second = second -> next;
if (second) second = second -> next;
else return 0;
if (first == second)
{
first = head;
while (first != second)
{
first = first -> next;
second = second -> next;
}
return first;
}
}
return 0;
}
};