判断单链表是否存在环,判断两个链表是否相交问题详解

来源:互联网 发布:心事谁人知歌词意思 编辑:程序博客网 时间:2024/06/04 01:13

 

有一个单链表,其中可能有一个环,也就是某个节点的next指向的是链表中在它之前的节点,这样在链表的尾部形成一环。

问题:

1、如何判断一个链表是不是这类链表?
2、如果链表为存在环,如何找到环的入口点?

解答:

一、判断链表是否存在环,办法为:

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

bool IsExitsLoop(Node *head)
{
 Node* slow=head,*fast=head;
 while (fast&&  fast->next)
 {
  slow=slow->next;
  fast=fast->next->next;//由于是fast=fast->next->next,所以fast->next是不能为空
  if(slow == fast)
   break;
 }
 return !(fast == NULL|| fast->next==NULL  );

}

 

bool IsExitsLoop(Node *head){Node* slow=head,*fast=head;while (fast&&  fast->next){slow=slow->next;fast=fast->next->next;//由于是fast=fast->next->next,所以fast->next是不能为空if(slow == fast)break;}return !(fast == NULL|| fast->next==NULL  );}



 二.如果存在环的话,那么找出环的第一个节点:

当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)循环内环+相遇点到环入口点,于是我们从链表头、与相遇点分别设一个指针,每次各走一步,两个指针必定相遇,且相遇第一点为环入口点。程序描述如下:

 

// IsExitsLoop2.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
using namespace std;

typedef struct node
{
 int data;
 struct node* next;

}Node;


Node* create_Node_HaveRing(int a[],int len)
{
 Node* head,*p,*q;
 p=new Node();
 p->data=a[0];
 head=p;

 for (int i=1;i<len;i++)
 {
  q=new Node();
  q->data=a[i];
  p->next=q;
  p=q;
 }
 p->next=head->next;
 return head;
}

Node* create_Node_NoRing(int a[],int len)
{
 Node* head,*p,*q;
 p=new Node();
 p->data=a[0];
 head=p;
 p=new Node();
 p->data=a[1];
 head->next=p;

 for (int i=2;i<len;i++)
 {
  q=new Node();
  q->data=a[i];
  p->next=q;
  p=q;
 }
 p=NULL;
 return head;
}


bool IsExitsLoop(Node *head)
{
 Node* 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);
}

Node* FindLoopPort(Node *head)
{
 Node* fast=head,*slow=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;
}

 

 

int _tmain(int argc, _TCHAR* argv[])
{
 int a[]={1,2,3,4,5,6,7,8,9,10};
 int len =sizeof(a)/sizeof(int);
 Node* head1 = create_Node_HaveRing(a,len);
 Node* head2 = create_Node_NoRing(a,len);
 bool isLoop = IsExitsLoop(head1);
 if (isLoop)
 {
  cout<<"head1找到环的入口:"<<endl;
  cout<<FindLoopPort(head1)->data<<endl;
 }
 else
 {
  cout<<"head1没有环"<<endl;
 }
 isLoop = IsExitsLoop(head2);
 if (isLoop)
 {
  cout<<"head2找到环的入口:"<<endl;
  cout<<FindLoopPort(head2)->data<<endl;
 }
 else
 {
  cout<<"head2没有找到环"<<endl;
 }
 
 system("pause");
 return 0;
}

 

代码:

// IsExitsLoop2.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include <iostream>using namespace std;typedef struct node{int data;struct node* next;}Node;Node* create_Node_HaveRing(int a[],int len){Node* head,*p,*q;p=new Node();p->data=a[0];head=p;for (int i=1;i<len;i++){q=new Node();q->data=a[i];p->next=q;p=q;}p->next=head->next;return head;}Node* create_Node_NoRing(int a[],int len){Node* head,*p,*q;p=new Node();p->data=a[0];head=p;p=new Node();p->data=a[1];head->next=p;for (int i=2;i<len;i++){q=new Node();q->data=a[i];p->next=q;p=q;}p=NULL;return head;}bool IsExitsLoop(Node *head){Node* 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);}Node* FindLoopPort(Node *head){Node* fast=head,*slow=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;}int _tmain(int argc, _TCHAR* argv[]){int a[]={1,2,3,4,5,6,7,8,9,10};int len =sizeof(a)/sizeof(int);Node* head1 = create_Node_HaveRing(a,len);Node* head2 = create_Node_NoRing(a,len);bool isLoop = IsExitsLoop(head1);if (isLoop){cout<<"head1找到环的入口:"<<endl;cout<<FindLoopPort(head1)->data<<endl;}else{cout<<"head1没有环"<<endl;}isLoop = IsExitsLoop(head2);if (isLoop){cout<<"head2找到环的入口:"<<endl;cout<<FindLoopPort(head2)->data<<endl;}else{cout<<"head2没有找到环"<<endl;}system("pause");return 0;}


 

 



 

 

原创粉丝点击