-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathll_problem_detect_loop.c
75 lines (59 loc) · 1.2 KB
/
ll_problem_detect_loop.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
int data;
struct Node* next;
}Node;
/*This function return 0 if there is no loop, it returns 1 if a loop is detected*/
int detect_loop(Node* ptrHead )
{
Node* slow = ptrHead;
Node* fast = ptrHead;
while( slow != NULL && fast!=NULL )
{
slow = slow->next;
fast = fast->next->next;
if( slow == fast )
{
return 1;
}
}
return 0;
}
int main()
{
int result = -1;
Node *head = NULL;
Node node1, node2, node3, node4;
node1.data = 11;
node1.next = &node2;
node2.data = 22;
node2.next = &node3;
node3.data = 33;
node3.next = &node4;
node4.data = 44;
node4.next = NULL;//&node2;
head = &node1;
result = detect_loop( head );
switch( result )
{
case 0:
{
printf("\nLoop is NOT detected in the given linked list.\n");
}
break;
case 1:
{
printf("\nLoop is detected in the given linked list.\n");
}
break;
default:
{
printf("\nInvalid result...\n");
}
};
printf("\n\n");
return 0;
}
//end of file