原文链接
http://www.cnblogs.com/shuaiwhu/archive/2012/05/03/2480509.html
如图,如果单链表有环,则在遍历时,在通过6之后,会重新回到3,那么我们可以在遍历时使用两个指针,看两个指针是否相等。
方法一:使用p、q两个指针,p总是向前走,但q每次都从头开始走,对于每个节点,看p走的步数是否和q一样。如图,当p从6走到3时,用了6步,此时若q从head出发,则只需两步就到3,因而步数不等,出现矛盾,存在环
方法二:使用p、q两个指针,p每次向前走一步,q每次向前走两步,若在某个时候p == q,则存在环。
#include <stdio.h>
#include <stdlib.h>
#define LEN 8
typedef struct node* node_t;
struct node{
char val;
struct node *next;
};
//method 1
int has_loop(struct node *head);
//method 2
int has_loop2(node_t head);
int main()
{
node_t* arr = (node_t*)malloc(sizeof(struct node)*LEN);
arr[0] = (node_t)malloc(sizeof(struct node));
int i;
for(i = 1; i < LEN; i++)
{
arr[i] = (node_t)malloc(sizeof(struct node));
arr[i - 1]->next = arr[i];
}
arr[LEN - 1]->next = NULL;
//you can add a loop here to test
//arr[6]->next = arr[0];
if (has_loop(arr[0]))
printf("method1: has loop.\n");
else
printf("method1: has no loop.\n");
if (has_loop2(arr[0]))
printf("method2: has loop.\n");
else
printf("method2: has no loop.\n");
return 0;
}
//if two pointer are equal, but they don't have the same steps, then has a loop
int has_loop(node_t head)
{
node_t cur1 = head;
int pos1 = 0;
while(cur1){
node_t cur2 = head;
int pos2 = 0;
pos1 ++;
while(cur2){
pos2 ++;
if(cur2 == cur1){
if(pos1 == pos2)
break;
else
return 1;
}
cur2 = cur2->next;
}
cur1 = cur1->next;
}
return 0;
}
//using step1 and step2 here
//if exists a loop, then the pointer which use step2 will catch up with the pointer which uses step1
int has_loop2(node_t head)
{
node_t p = head;
node_t q = head;
while (p != NULL && q != NULL)
{
/*
p = p->next;
if (q->next != NULL)
q = q->next->next;
if (p == q)
return 1;
*/
//correct it on 17/11/2012
p = p->next;
q = q->next;
if (q != NULL)
q = q->next;
if (p != NULL && p == q)
return 1;
}
return 0;
}
=========================================
原文链接
http://www.cnblogs.com/ccdev/archive/2012/09/06/2673618.html
1、先判断是否有环
设置两个指针(fast, slow),初始值都指向头,slow每次前进一步,fast每次前进二步,如果链表存在环,则fast必定先进入环,而slow后进入环,两个指针必定相遇。(当然,fast先行头到尾部为NULL,则为无环链表)
bool isExitsLoop(list *head)
{
list *slow = head, *fast = head;
while ( fast && fast->next )
{
slow = slow->next;
fast = fast->next->next;
if ( slow == fast ) break;
}
return !(fast == NULL || fast->next == NULL);
}
此问题可扩展至:求循环链表任一节点“对面的”(最远端)的节点,算法同上,当fast到达head(此处head为任一节点)或head->next时,slow指示的就是最远端的节点。具体code略。
2、经过第1步确认存在环后,寻找环入口点:
算法描述:
当fast若与slow相遇时,slow肯定没有走遍历完链表,而fast已经在环内循环了n圈(1<=n)。假设slow走了s步,则fast走了2s步(fast步数还等于s 加上在环上多转的n圈),设环长为r,则:
2s = s + nr
s= nr
设整个链表长L,入口环与相遇点距离为x,起点到环入口点的距离为a。
a + x = nr
a + x = (n – 1)r +r = (n-1)r + L - a
a = (n-1)r + (L – a – x)
(L – a – x)为相遇点到环入口点的距离,由此可知,从链表头到环入口点等于(n-1)循环内环+相遇点到环入口点,于是我们从链表头、与相遇点分别设一个指针,每次各走一步,两个指针必定相遇,且相遇第一点为环入口点。
list* FindLoopNode(list* head)
{
list *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;
}
此问题可扩展至:判断两个单链表是否相交,如果相交,给出相交的第一个点(两个链表都不存在环)。
根据问题描述,两个单链表自相交点起,将合并为一个单链表,这是理解算法的关键。
算法描述:
将其中一个链表首尾相连,检测另外一个链表是否存在环,如果存在,则两个链表相交,而检测出来的依赖环入口即为相交的第一个点。具体code略。