-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathctci-linked-list-cycle.cpp
48 lines (42 loc) · 1.22 KB
/
ctci-linked-list-cycle.cpp
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
// Linked Lists: Detect a Cycle
// Given a pointer to the head of a linked list, determine whether the linked list loops back onto itself (i.e., determine if the list ends in a circularly linked list).
//
// https://www.hackerrank.com/challenges/ctci-linked-list-cycle/problem
//
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
using namespace std;
struct Node
{
int data;
Node* next;
};
#include "ctci-linked-list-cycle.hpp"
int main()
{
Node *A, *B, *C, *D,*E,*F;
A = new Node(); B= new Node(); C= new Node(); D = new Node(); E = new Node(); F= new Node();
// case 1: NULL list
if(has_cycle(NULL)) cout<<true<<endl;
else cout<<false<<endl;
//case 2:
A->next = B;
B->next = C;
C->next = A;
if(has_cycle(A)) cout<<true<<endl;
else cout<<false<<endl;
//case 3:
A->next = B; B->next = C; C->next = D; D->next = E; E->next = F; F->next = E;
if(has_cycle(A)) cout<<true<<endl;
else cout<<false<<endl;
//case 4:
A->next = B; B->next = C; C->next = D; D->next = E; E->next = F; F->next = NULL;
if(has_cycle(A)) cout<<true<<endl;
else cout<<false<<endl;
// case 5:
A->next = B; B->next = C; C->next = D; D->next = E; E->next = F; F->next = A;
if(has_cycle(A)) cout<<true<<endl;
else cout<<false<<endl;
}