forked from amritanand-py/cps02
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathO M01 - Looped List.c
62 lines (54 loc) · 1.17 KB
/
O M01 - Looped List.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
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include "string.h"
struct Node{
int value;
struct Node* next;
};
typedef struct Node Node;
Node links[105];
void add_link(int from, int to){
links[from].next = &links[to];
}
void set_value(int index, int val){
links[index].value = val;
}
//BODY BEGINS HERE
int find_loop(struct Node* head){
/***
Your Code Here
It must return 1 if a loop exists and zero otherwise.
***/
struct Node *slow = head;
struct Node *fast = head;
while(fast != NULL && fast->next != NULL)
{
slow = slow->next;
fast = fast->next->next;
if (slow==fast)
return 1;
}
return 0;
}
/***
Your Code Here
It must return 1 if a loop exists and zero otherwise.
***/
//BODY ENDS HERE
//TAIL BEGINS HERE
int main(){
int i,n,m,tmp,tmp1,tmp2;
scanf("%d %d", &n, &m);
for(i = 0; i < n; i++){
scanf("%d", &tmp);
set_value(i, tmp);
}
for(i = 0; i < m; i++){
scanf("%d %d", &tmp1, &tmp2);
add_link(tmp1,tmp2);
}
if(find_loop(&links[0]) == 1) printf("Loop Found");
else printf("No Loop Found");
return 0;
}