Does a Nodelist contain circle?

Algorithm Summary:

Determine whether a Nodelist contains circle

1.The Nodelist has no node,return 0

2.The Nodelist has one node and the next field point to NULL return false

3.The Nodelist has more than one Nodes(at least two nodes)

 1)define two point ,p1 and p2,initially both point to head of the list;

 2)Move p1 one step and p2 two steps each time,if the list has circle,p1 and p2 will point to the same node after several steps.

 3)if p2 next field point to NULL after several steps,it indicates that there is no circle in the list.

Implement Code

Code
 1 typedef int dataType;
 2 typedef struct Node{
 3    dataType data;
 4    struct Nodelist * next;
 5 } Nodelist;
 6 
 7 int IsCircleInList(Nodelist * list){
 8       LinkList * p1 = list;
 9     LinkList * p2= list;
10     while(node->next!=NULL  && node2->next->next!=NULL)
11     {
12         p1 = node->next;
13         p2 = node2->next->next;
14         if(p1==p2) return 1;
15     }
16 
17     return 0;
18 

你可能感兴趣的:(Does a Nodelist contain circle?)