判断一个单向链表中是否有环
2015-01-27 10:06
211 查看
判断一个单向链表中是否有环
来自于:http://blog.sina.com.cn/s/blog_5f0d72800100tayr.html
思路:
用两个指针,pSlow,pFast,就是一个慢一个快
慢的一次跳一步
快的一次跳两步
往链表末端移动。如果pFast==NULL,则说明链表没有环,如果pSlow==pFast,则说明链表存在环。
这个方法与前面2个方法相比,不但速度很快,而且不需要额外的存储空间,时间复杂度、空间复杂度都是最小的。
bool IsLoop(node *head)
{
node *pSlow=head;
node *pFast=head;
while(pSlow!=NULL&&pFast!=NULL)
{
pSlow=pSlow->next;
pFast=pFast->next->next;
if(pSlow==pFast)
returntrue;
}
returnfalse;
}
完整的测试代码如下:
#include "iostream"
using namespace std;
struct node
{
int data;
structnode *next;
}*linklist,*s,*head;
map<node*,int>m;
bool IsLoop(node *head)
{
node *pSlow=head;
node *pFast=head;
while(pSlow!=NULL &&pFast!=NULL)
{
pSlow=pSlow->next;
pFast=pFast->next->next;
if(pSlow==pFast)
returntrue;
}
return false;
}
node* InsertNode(node *head,int value)
{
if(head==NULL)
{
head=(node*)malloc(sizeof(node));
if(head==NULL)
printf("mallocfailed");
else
{
head->data=value;
head->next=NULL;
}
}
else
{
node*temp=(node *)malloc(sizeof(node));
if(temp==NULL)
printf("mallocfailed");
else
{
temp->data=value;
temp->next=head;
head=temp;
}
}
returnhead;
}
int main(void)
{
node *t,*q,*p=NULL;
p=InsertNode(p,8);
p=InsertNode(p,7);
p=InsertNode(p,6);
p=InsertNode(p,5);
p=InsertNode(p,4);
q=p;
p=InsertNode(p,3);
p=InsertNode(p,2);
p=InsertNode(p,1);
t=p;
while(t->next) // 找到链表的尾指针
t=t->next;
t->next=q; //将链表的尾指针指向第四个节点,这样就构成了一个环
bool flag=IsLoop(p);
if(flag)
cout<<"这个链表存在一个环"<<endl;
else
cout<<"这个链表不存在一个环"<<endl;
system("pause");
return 0;
}
1->2->3->4->5->6->7->8 最后8又指向了4,构成了一个环
来自于:http://blog.sina.com.cn/s/blog_5f0d72800100tayr.html
思路:
用两个指针,pSlow,pFast,就是一个慢一个快
慢的一次跳一步
快的一次跳两步
往链表末端移动。如果pFast==NULL,则说明链表没有环,如果pSlow==pFast,则说明链表存在环。
这个方法与前面2个方法相比,不但速度很快,而且不需要额外的存储空间,时间复杂度、空间复杂度都是最小的。
bool IsLoop(node *head)
{
node *pSlow=head;
node *pFast=head;
while(pSlow!=NULL&&pFast!=NULL)
{
pSlow=pSlow->next;
pFast=pFast->next->next;
if(pSlow==pFast)
returntrue;
}
returnfalse;
}
完整的测试代码如下:
#include "iostream"
using namespace std;
struct node
{
int data;
structnode *next;
}*linklist,*s,*head;
map<node*,int>m;
bool IsLoop(node *head)
{
node *pSlow=head;
node *pFast=head;
while(pSlow!=NULL &&pFast!=NULL)
{
pSlow=pSlow->next;
pFast=pFast->next->next;
if(pSlow==pFast)
returntrue;
}
return false;
}
node* InsertNode(node *head,int value)
{
if(head==NULL)
{
head=(node*)malloc(sizeof(node));
if(head==NULL)
printf("mallocfailed");
else
{
head->data=value;
head->next=NULL;
}
}
else
{
node*temp=(node *)malloc(sizeof(node));
if(temp==NULL)
printf("mallocfailed");
else
{
temp->data=value;
temp->next=head;
head=temp;
}
}
returnhead;
}
int main(void)
{
node *t,*q,*p=NULL;
p=InsertNode(p,8);
p=InsertNode(p,7);
p=InsertNode(p,6);
p=InsertNode(p,5);
p=InsertNode(p,4);
q=p;
p=InsertNode(p,3);
p=InsertNode(p,2);
p=InsertNode(p,1);
t=p;
while(t->next) // 找到链表的尾指针
t=t->next;
t->next=q; //将链表的尾指针指向第四个节点,这样就构成了一个环
bool flag=IsLoop(p);
if(flag)
cout<<"这个链表存在一个环"<<endl;
else
cout<<"这个链表不存在一个环"<<endl;
system("pause");
return 0;
}
1->2->3->4->5->6->7->8 最后8又指向了4,构成了一个环
相关文章推荐
- 判断一个单向链表中是否存在环
- Linked List Cycle-- 判断一个单向链表中是否有环存在
- 判断一个单向链表是否有环和判断一个单向链表是否是循环链表
- 判断一个单向链表中是否有环
- 判断一个单向链表是否有环,算法证明
- 如何判断一个单向链表是否有环路?
- 剑指-判断一个单向链表是否形成了环形结构
- 如何判断一个单向链表是否为回文链表(Palindrome Linked List)
- 状下算法可以判断一个单向链表中是否有环
- 判断一个单向链表是否有环和判断一个单向链表是否是循环链表
- 剑指offer面试题15——扩展2:判断一个单向链表是否形成环结构
- 判断一个单向链表中是否有环
- 如何判断一个单向链表是否存在循环的经典方法
- 判断一个单向链表中是否有环
- 判断一个单向链表中是否有环
- 判断一个单向链表中是否有环
- 判断一个单向链表是否形成了环状结构
- 判断一个单向链表是否有环
- 判断一个单向链表是否有环
- 题目:①判断一个单向链表是否有环,如果有环则找到环的入口节点。 ②判断两个单向链表是否相交,如果相交则找到交点节点。