-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathCycleDetection.cs
More file actions
154 lines (121 loc) · 4 KB
/
CycleDetection.cs
File metadata and controls
154 lines (121 loc) · 4 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// https://www.hackerrank.com/challenges/detect-whether-a-linked-list-contains-a-cycle/problem
// The test cases are wrong, so if you check the data instead of objects it fails in hidden test cases.
// This file contains tree solutions
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System;
class Solution {
class SinglyLinkedListNode {
public int data;
public SinglyLinkedListNode next;
public SinglyLinkedListNode(int nodeData) {
this.data = nodeData;
this.next = null;
}
}
class SinglyLinkedList {
public SinglyLinkedListNode head;
public SinglyLinkedListNode tail;
public SinglyLinkedList() {
this.head = null;
this.tail = null;
}
public void InsertNode(int nodeData) {
SinglyLinkedListNode node = new SinglyLinkedListNode(nodeData);
if (this.head == null) {
this.head = node;
} else {
this.tail.next = node;
}
this.tail = node;
}
}
static void PrintSinglyLinkedList(SinglyLinkedListNode node, string sep, TextWriter textWriter) {
while (node != null) {
textWriter.Write(node.data);
node = node.next;
if (node != null) {
textWriter.Write(sep);
}
}
}
// Complete the hasCycle function below.
/*
* For your reference:
*
* SinglyLinkedListNode {
* int data;
* SinglyLinkedListNode next;
* }
*
*/
static bool hasCycle(SinglyLinkedListNode head)
{
var slow = head;
var fast = head;
while (fast != null && fast.next != null)
{
slow = slow.next;
fast = fast.next.next;
if (slow == fast) return true;
}
return false;
}
static Dictionary<SinglyLinkedListNode, bool> Cache = new Dictionary<SinglyLinkedListNode, bool>();
static bool hasCycle2(SinglyLinkedListNode head) {
while(head != null)
{
if(Cache.ContainsKey(head)) return true;
Cache.Add(head, true);
head = head.next;
}
return false;
}
static bool hasCycle3(SinglyLinkedListNode head) {
if(head == null) return false;
else if(Cache.ContainsKey(head.data)) return true;
else
{
Cache.Add(head.data, true);
return hasCycle(head.next);
}
}
static void Main(string[] args) {
TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);
int tests = Convert.ToInt32(Console.ReadLine());
for (int testsItr = 0; testsItr < tests; testsItr++) {
int index = Convert.ToInt32(Console.ReadLine());
SinglyLinkedList llist = new SinglyLinkedList();
int llistCount = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i < llistCount; i++) {
int llistItem = Convert.ToInt32(Console.ReadLine());
llist.InsertNode(llistItem);
}
SinglyLinkedListNode extra = new SinglyLinkedListNode(-1);
SinglyLinkedListNode temp = llist.head;
for (int i = 0; i < llistCount; i++) {
if (i == index) {
extra = temp;
}
if (i != llistCount-1) {
temp = temp.next;
}
}
temp.next = extra;
bool result = hasCycle(llist.head);
textWriter.WriteLine((result ? 1 : 0));
}
textWriter.Flush();
textWriter.Close();
}
}