65. 链表是否带环、环入口、环长度、链表相交问题分析与总结

【本文链接】

http://www.cnblogs.com/hellogiser/p/linked-list-loop-and-intersections.html

(1)链表是否有环?

设置两个指针(fast, slow),初始值都指向头,slow每次前进一步,fast每次前进二步,如果链表存在环,则fast必定先进入环,而slow后进入环,两个指针必定相遇,设碰撞点为p。(当然,fast如果为NULL,则为无环链表)程序如下:

【代码】

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 
/*
    version: 1.0
    author: hellogiser
    blog: http://www.cnblogs.com/hellogiser
    date: 2014/5/30
*/

bool IsExitsLoop(slist *head)
{
    slist *slow = head, *fast = head;
    
while ( fast && fast->next )
    {
        slow = slow->next;
        fast = fast->next->next;
        
if ( slow == fast ) break;
    }

    
if (fast == NULL || fast->next == NULL)
        
return false;

    
return true;
}

(2)找到环的入口点?

定理:slow和fast相遇点为p,让slow从head开始,fast从p开始,每次往后各走一步,直到slow和fast再次相遇,则相遇点即为环的入口。

证明:

当fast若与slow相遇时,slow肯定没有走遍历完链表,而fast已经在环内循环了n圈(n>=1)。假设slow走了s步,则fast走了2s步(fast步数还等于s 加上在环上多转的n圈),设环长为r,则:

2s = s + nr 即:s= nr

设整个链表长L,环入口与相遇点距离为x,起点到环入口点的距离为a。

则s=a+x, L=a+r。那么a + x = nr = (n – 1)r +r = (n-1)r + L - a,则有a = (n-1)r + (L – a – x)。

(L–a–x)为相遇点到环入口点的距离,由此可知,从链表头到环入口点等于(n-1)循环内环+相遇点到环入口点,于是我们从链表头、相遇点分别设一个指针,每次各走一步,两个指针必定相遇,且相遇第一点为环入口点。

【代码】

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 
/*
    version: 1.0
    author: hellogiser
    blog: http://www.cnblogs.com/hellogiser
    date: 2014/5/30
*/

slist *FindLoopPort(slist *head)
{
    slist *slow = head, *fast = head;

    
while ( fast && fast->next )
    {
        slow = slow->next;
        fast = fast->next->next;
        
if ( slow == fast ) break;
    }

    
if (fast == NULL || fast->next == NULL)
        
return NULL;

    slow = head;
    
while (slow != fast)
    {
        slow = slow->next;
        fast = fast->next;
    }

    
return slow;
}

(3)如何知道环的长度?

记录下碰撞点meet,slow、fast从该点开始,再次碰撞所走过的操作数就是环的长度r。

【代码】

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
 
/*
    version: 1.0
    author: hellogiser
    blog: http://www.cnblogs.com/hellogiser
    date: 2014/5/30
*/

unsigned int GetLoopLength(slist *head)
{
    slist *slow = head, *fast = head;

    
while ( fast && fast->next )
    {
        slow = slow->next;
        fast = fast->next->next;
        
if ( slow == fast ) break;
    }

    
if (fast == NULL || fast->next == NULL)
        
return 0;

    slist *meet = slow;
    slow = meet->next;
    fast = meet->next->next;
    
unsigned int len = 1;
    
while (slow != fast)
    {
        len ++;
        slow = slow->next;
        fast = fast->next->next;
    }

    
return len;
}

(4)带环链表的长度是多少?

L=a+r。

(5)判断两个单链表是否相交?

判断两个单链表是否相交,如果相交,给出相交的第一个点(两个链表都不存在环)。

比较好的方法有两个:

一、将其中一个链表L2首尾相连,检测另外一个链表L1是否存在环,如果存在,则两个链表相交,而检测出来的依赖环入口即为相交的第一个点。

二、如果两个链表相交,那个两个链表从相交点到链表结束都是相同的节点,我们可以先遍历一个链表,直到尾部,再遍历另外一个链表,如果也可以走到同样的结尾点,则两个链表相交。这时我们记下两个链表length,再遍历一次,长链表节点先出发前进(lengthMax-lengthMin)步,之后两个链表同时前进,每次一步,相遇的第一点即为两个链表相交的第一个点。具体参见35.两链表的第一个公共结点[Find the first common node of two linked list]

【参考】

http://www.cnblogs.com/hellogiser/p/find-the-first-common-node-of-two-linked-list.html

http://www.cppblog.com/humanchao/archive/2012/11/12/47357.html

http://blog.csdn.net/liuxialong/article/details/6555850

【本文链接】

http://www.cnblogs.com/hellogiser/p/linked-list-loop-and-intersections.html