# C/C++经典面试题之判断链表是否有环

#include <cstdio>

typedef struct list
{
int data;
struct list *next;
}LIST;

/* Method 1: check the occurrence of p->next from head to p */
{
LIST *p = head, *q= NULL;

if (p == NULL)
return false;

while (p->next)
{
/* check whether p points to itself */
if (p->next == p)
{
return true;
}

/* check the occurrence of p->next in head to p */
while (q != p)
{
if (q == p->next)
{
return true;
}
q = q->next;
}
p = p->next;
}
/* p->next is NULL, not a circle */
return false;
}

/* Method 2: q goes faster than p, if at last p == q, means there's circle */
/* 优点：逻辑上简单。 缺点：无法具体知道从哪个点拆开该圈 */
{
LIST *p, *q;

if (p == NULL)
return false;

q = p->next;

while (p != NULL && q != NULL)
{
if (p == q)
{
return true;
}
p = p->next;
if (q->next == NULL)
{
return 0;
}
else
{
q=q->next->next;
}
}
return 0;
}
void testIS()
{
LIST a, b, c, *head = &a;
a.next = &b;
b.next = &c;
c.next = &a;
printf("1:is circle | 0:not circle || result check_circle_1(head):\
printf("1:is circle | 0:not circle || result check_circle_1(head):\
}
void testNO()
{
LIST a, b, c, *head = &a;
a.next = &b;
b.next = &c;
c.next = NULL;
printf("1:is circle | 0:not circle || result check_circle_1(head):\
printf("1:is circle | 0:not circle || result check_circle_1(head):\
}
int main()
{
printf("-------testIS()--------\n");
testIS();
printf("-------testNO()--------\n");
testNO();
}
/****************************************

-------testIS()--------
1:is circle | 0:not circle || result check_circle_1(head):        1
1:is circle | 0:not circle || result check_circle_1(head):        1
-------testNO()--------
1:is circle | 0:not circle || result check_circle_1(head):        0
1:is circle | 0:not circle || result check_circle_1(head):        0

Process returned 0 (0x0)   execution time : 0.078 s
Press any key to continue.

*****************************************/



#include <cstdio>

typedef struct list
{
int data;
struct list *next;
}LIST;

/* Method 1: check the occurrence of p->next from head to p */
{
LIST *p = head, *q= NULL;

if (p == NULL)
return false;

while (p->next)
{
/* check whether p points to itself */
if (p->next == p)
{
return true;
}

/* check the occurrence of p->next in head to p */
while (q != p)
{
if (q == p->next)
{
return true;
}
q = q->next;
}
p = p->next;
}
/* p->next is NULL, not a circle */
return false;
}

/* Method 2: q goes faster than p, if at last p == q, means there's circle */
/* 优点：逻辑上简单。 缺点：无法具体知道从哪个点拆开该圈 */
{
LIST *p, *q;

if (p == NULL)
return false;

q = p->next;

while (p != NULL && q != NULL)
{
if (p == q)
{
return true;
}
p = p->next;
if (q->next == NULL)
{
return 0;
}
else
{
q=q->next->next;
}
}
return 0;
}
void testIS()
{
LIST a, b, c, *head = &a;
a.next = &b;
b.next = &c;
c.next = &a;
printf("1:is circle | 0:not circle || result check_circle_1(head):\
printf("1:is circle | 0:not circle || result check_circle_1(head):\
}
void testNO()
{
LIST a, b, c, *head = &a;
a.next = &b;
b.next = &c;
c.next = NULL;
printf("1:is circle | 0:not circle || result check_circle_1(head):\
printf("1:is circle | 0:not circle || result check_circle_1(head):\
}
int main()
{
printf("-------testIS()--------\n");
testIS();
printf("-------testNO()--------\n");
testNO();
}
/****************************************

-------testIS()--------
1:is circle | 0:not circle || result check_circle_1(head):        1
1:is circle | 0:not circle || result check_circle_1(head):        1
-------testNO()--------
1:is circle | 0:not circle || result check_circle_1(head):        0
1:is circle | 0:not circle || result check_circle_1(head):        0

Process returned 0 (0x0)   execution time : 0.078 s
Press any key to continue.

*****************************************/

#### C语言中怎么判断单链表中是否有环？

2017-11-04 15:43:52

#### 判断单链表中是否有环，计算出环的首地址 C语言实现

2014-06-24 12:44:24

#### leetcode OJ 判断单链表中是否有环

2014-10-23 18:48:20

#### 剑指offer之面试题15-2：单链表是否有环

2016-04-23 15:35:07

#### 【C++编程题】高效判断单链表是否有环以及单链表的创建

2017-10-13 18:41:39

#### 判断一个单链表是否有环

2015-04-19 14:01:25

#### 题目：代码实现判断单链表是否有环

2015-03-29 22:45:43

#### 判断单链表是否有环（C++）

2016-01-21 15:35:46

#### 判断单向链表中是否有环和查找环的入口

2015-08-01 13:24:30

#### 判断链表是否带环，以及环的入口

2016-06-24 13:39:19