链表中环的入口结点

 

一个链表中包含环,请找出该链表的环的入口结点。要求不能使用额外的空间。

 

使用双指针,一个指针 fast 每次移动两个节点,一个指针 slow 每次移动一个节点。因为存在环,所以两个指针必定相遇在环中的某个节点上。假设相遇点在下图的 z1 位置,此时 fast 移动的节点数为 x+2y+z,slow 为 x+y,由于 fast 速度比 slow 快一倍,因此 x+2y+z=2(x+y),得到 x=z。

在相遇点,slow 要到环的入口点还需要移动 z 个节点,如果让 fast 重新从头开始移动,并且速度变为每次移动一个节点,那么它到环入口点还需要移动 x 个节点。在上面已经推导出 x=z,因此 fast 和 slow 将在环入口点相遇。

 

 

C++:

 1 /*
 2 struct ListNode {
 3     int val;
 4     struct ListNode *next;
 5     ListNode(int x) :
 6         val(x), next(NULL) {
 7     }
 8 };
 9 */
10 class Solution {
11 public:
12     ListNode* EntryNodeOfLoop(ListNode* pHead)
13     {
14         if (pHead == NULL || pHead->next == NULL)
15             return NULL ;
16         ListNode* fast = pHead ;
17         ListNode* slow = pHead ;
18         do{
19             fast = fast->next->next ;
20             slow = slow->next ;
21         }while(fast != slow) ;
22         fast = pHead ;
23         while(fast != slow){
24             fast = fast->next ;
25             slow = slow->next ;
26         }
27         return fast ;
28     }
29 };

 

posted @ 2019-04-01 23:28  __Meng  阅读(156)  评论(0编辑  收藏  举报