/* struct ListNode { int val; struct ListNode next; ListNode(int x) : val(x), next(NULL) { } }; / class Solution { public: ListNode EntryNodeOfLoop(ListNode pHead) { if(!pHead) return nullptr; if(!(pHead -> next)) return nullptr; ListNode* fast = pHead; ListNode* slow = pHead;
bool flag = 0;
while(slow -> next && fast -> next && fast -> next -> next)
{
slow = slow -> next;
fast = fast -> next -> next;
if(slow == fast)
{
flag = 1;
break;
}
}
if(flag)
{
auto low = pHead;
while(low != fast)
{
fast = fast -> next;
low = low -> next;
}
return low;
}
return nullptr;
}
};

京公网安备 11010502036488号