链表中环的入口结点
链表中环的入口结点
https://www.nowcoder.com/practice/253d2c59ec3e4bc68da16833f79a38e4?tpId=13&tqId=11208&rp=3&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
参考思考:https://blog.nowcoder.net/n/deaa284f105e48f49f38b5d7cb809cd7
代码:
/* struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) { } }; */ class Solution { public: ListNode* EntryNodeOfLoop(ListNode* pHead) { if(pHead==NULL||pHead->next==NULL) { return NULL; } ListNode* fast = pHead; ListNode* slow = pHead; while(fast!=NULL&&fast->next!=NULL) { fast = fast->next->next; slow = slow->next; if(fast==slow) { ListNode* slow2 = pHead; while(slow2!=slow) { slow2 = slow2->next; slow = slow->next; } return slow2; } } return NULL; } };