Skip to content

Commit b03fa0d

Browse files
author
Gonzalo Diaz
committed
[Hacker Rank] Interview Preparation Kit: Linked Lists: Detect a Cycle. Solved ✓.
1 parent b37d9d5 commit b03fa0d

File tree

3 files changed

+205
-0
lines changed

3 files changed

+205
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
namespace algorithm_exercises_csharp.hackerrank.interview_preparation_kit;
2+
3+
[TestClass]
4+
public class LinkedListCycleTest
5+
{
6+
class LinkedListCycleTestCase
7+
{
8+
public string title = "";
9+
public LinkedList.Node? llist;
10+
public bool expected;
11+
}
12+
13+
private static LinkedListCycleTestCase[] tests = [];
14+
15+
[TestInitialize]
16+
public void testInitialize()
17+
{
18+
// Linked list sample data:
19+
LinkedList.Node ll1_1 = new(1);
20+
LinkedList.Node ll1_2 = new(2);
21+
LinkedList.Node ll1_3 = new(3);
22+
LinkedList.Node ll1_4 = new(4);
23+
LinkedList.Node ll1_5 = new(5);
24+
25+
ll1_1.next = ll1_2;
26+
ll1_2.next = ll1_3;
27+
ll1_3.next = ll1_4;
28+
ll1_4.next = ll1_5;
29+
ll1_4.next = ll1_3; // <- cycle
30+
31+
LinkedList.Node ll2_1 = new(1);
32+
LinkedList.Node ll2_2 = new(2);
33+
LinkedList.Node ll2_3 = new(3);
34+
35+
ll2_1.next = ll2_2;
36+
ll2_2.next = ll2_3;
37+
38+
tests = [
39+
new()
40+
{
41+
title = "Sample Test case X",
42+
llist = ll1_1,
43+
expected = true
44+
},
45+
new()
46+
{
47+
title = "Sample Test case Y",
48+
llist = ll2_1,
49+
expected = false
50+
}
51+
];
52+
}
53+
54+
[TestMethod]
55+
public void testLinkedListCycle()
56+
{
57+
bool result;
58+
59+
foreach (LinkedListCycleTestCase test in tests)
60+
{
61+
result = LinkedListCycle.hasCycle(test.llist);
62+
Assert.AreEqual(
63+
test.expected,
64+
result,
65+
String.Format(
66+
"{0} testLinkedListCycle({1}) => must be: {2}",
67+
test.title,
68+
test.llist,
69+
test.expected
70+
)
71+
);
72+
}
73+
}
74+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// @link Problem definition [[docs/hackerrank/interview_preparation_kit/linked_lists/ctci_linked_list_cycle.md]]
2+
3+
namespace algorithm_exercises_csharp.hackerrank.interview_preparation_kit;
4+
5+
using System.Diagnostics.CodeAnalysis;
6+
7+
public class LinkedListCycle
8+
{
9+
[ExcludeFromCodeCoverage]
10+
protected LinkedListCycle() { }
11+
12+
public static bool hasCycle(LinkedList.Node? head)
13+
{
14+
List<LinkedList.Node> llindex = [];
15+
16+
LinkedList.Node? node = head;
17+
18+
while (node != null)
19+
{
20+
if (llindex.Contains(node))
21+
{
22+
return true;
23+
}
24+
25+
llindex.Add(node);
26+
node = node.next;
27+
}
28+
29+
return false;
30+
}
31+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# [Linked Lists: Detect a Cycle](https://www.hackerrank.com/challenges/ctci-linked-list-cycle)
2+
3+
- Difficulty: `#easy`
4+
- Category: `#ProblemSolvingIntermediate`
5+
6+
A linked list is said to contain a cycle if any node is visited more
7+
than once while traversing the list.
8+
For example, in the following graph there is a cycle formed
9+
when node `5` points back to node `3`.
10+
11+
```mermaid
12+
flowchart LR
13+
14+
1([1])
15+
2([2])
16+
3([3])
17+
4([4])
18+
5([5])
19+
20+
1 --> 2
21+
2 --> 3
22+
3 --> 4
23+
4 --> 5
24+
5 --> 3
25+
26+
classDef default color:#000,fill:#97FE9C,stroke:#488A53,stroke-width:4px;
27+
```
28+
29+
## Function Description
30+
31+
Complete the function has_cycle in the editor below.
32+
It must return a boolean true if the graph contains a cycle, or false.
33+
34+
has_cycle has the following parameter(s):
35+
36+
- head: a pointer to a Node object that points to the head of a linked list.
37+
38+
## Returns
39+
40+
- boolean: True if there is a cycle, False if there is not
41+
42+
**Note**: If the list is empty, `head` will be null.
43+
44+
## Input Format
45+
46+
There is no input for this challenge.
47+
A random linked list is generated at runtime and passed to your function.
48+
49+
## Constraints
50+
51+
- $ 0 \leq \text{list size} \leq 100$
52+
53+
## Sample Input
54+
55+
The following linked lists are passed as arguments to your function:
56+
57+
```mermaid
58+
flowchart LR
59+
60+
1([1])
61+
1 --> NULL
62+
63+
classDef default color:#000,fill:#97FE9C,stroke:#488A53,stroke-width:4px;
64+
65+
classDef none color: #DDD, fill:#FFFFFF00,fill:#FFFFFF00, stroke:#FFFFFF00, stroke-width: 0;
66+
67+
class NULL none
68+
```
69+
70+
```mermaid
71+
flowchart LR
72+
73+
1([1])
74+
2([2])
75+
3([3])
76+
77+
1 --> 2
78+
2 --> 3
79+
3 --> 2
80+
81+
classDef default color:#000,fill:#97FE9C,stroke:#488A53,stroke-width:4px;
82+
83+
classDef none color: #DDD, fill:#FFFFFF00,fill:#FFFFFF00, stroke:#FFFFFF00, stroke-width: 0;
84+
85+
class NULL none
86+
```
87+
88+
## Sample Output
89+
90+
```text
91+
0
92+
1
93+
```
94+
95+
## Explanation
96+
97+
1. The first list has no cycle, so we return false and the
98+
hidden code checker prints 0 to stdout.
99+
2. The second list has a cycle, so we return true and the
100+
hidden code checker prints 1 to stdout.

0 commit comments

Comments
 (0)