File tree Expand file tree Collapse file tree 3 files changed +130
-0
lines changed
algorithm-exercises-csharp-test/src/hackerrank/interview_preparation_kit/linked_list
algorithm-exercises-csharp/src/hackerrank/interview_preparation_kit/linked_list Expand file tree Collapse file tree 3 files changed +130
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
1
+ namespace algorithm_exercises_csharp . hackerrank . interview_preparation_kit ;
2
+
3
+ using System . Diagnostics . CodeAnalysis ;
4
+
5
+ public class LinkedListCycle
6
+ {
7
+ [ ExcludeFromCodeCoverage ]
8
+ protected LinkedListCycle ( ) { }
9
+
10
+ public static bool hasCycle ( LinkedList . Node ? head )
11
+ {
12
+ List < LinkedList . Node > llindex = [ ] ;
13
+
14
+ LinkedList . Node ? node = head ;
15
+
16
+ while ( node != null )
17
+ {
18
+ if ( llindex . Contains ( node ) )
19
+ {
20
+ return true ;
21
+ }
22
+
23
+ llindex . Add ( node ) ;
24
+ node = node . next ;
25
+ }
26
+
27
+ return false ;
28
+ }
29
+ }
Original file line number Diff line number Diff line change
1
+ namespace algorithm_exercises_csharp . hackerrank . interview_preparation_kit ;
2
+
3
+ public class LinkedList
4
+ {
5
+ public class Node ( int nodeData )
6
+ {
7
+ public int data { get ; set ; } = nodeData ;
8
+ public Node ? next { get ; set ; } = null ;
9
+ }
10
+
11
+ public static void printSinglyLinkedList ( Node node , string sep , TextWriter textWriter )
12
+ {
13
+ Node ? pointTo = node ;
14
+
15
+ while ( pointTo != null )
16
+ {
17
+ textWriter . Write ( pointTo . data ) ;
18
+
19
+ pointTo = pointTo . next ;
20
+
21
+ if ( pointTo != null )
22
+ {
23
+ textWriter . Write ( sep ) ;
24
+ }
25
+ }
26
+ }
27
+ }
You can’t perform that action at this time.
0 commit comments