Skip to content

Commit 00d7efb

Browse files
authored
Merge pull request #87 from sir-gon/feature/FindMergeNode
[Hacker Rank] Interview Preparation Kit: Linked Lists: Find Merge Poi…
2 parents 0f9988a + 70ff029 commit 00d7efb

File tree

3 files changed

+234
-0
lines changed

3 files changed

+234
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
namespace algorithm_exercises_csharp.hackerrank.interview_preparation_kit;
2+
3+
[TestClass]
4+
public class FindMergeNodeTest
5+
{
6+
class FindMergeNodeTestCase()
7+
{
8+
public string title = "";
9+
public LinkedList.Node llist1 = new(0);
10+
public LinkedList.Node llist2 = new(0);
11+
public int expected;
12+
}
13+
14+
private static FindMergeNodeTestCase[] tests = [];
15+
16+
[TestInitialize]
17+
public void testInitialize()
18+
{
19+
// Test Case 0
20+
//
21+
// 1
22+
// \
23+
// 2--->3--->NULL
24+
// /
25+
// 1
26+
//
27+
LinkedList.Node tc0_ll1_1 = new(1);
28+
LinkedList.Node tc0_ll1_2 = new(2);
29+
LinkedList.Node tc0_ll1_3 = new(3);
30+
tc0_ll1_1.next = tc0_ll1_2;
31+
tc0_ll1_2.next = tc0_ll1_3;
32+
33+
LinkedList.Node tc0_ll2_1 = new(1);
34+
tc0_ll2_1.next = tc0_ll1_2;
35+
36+
// Test Case 1
37+
//
38+
// 1--->2
39+
// \
40+
// 3--->Null
41+
// /
42+
// 1
43+
//
44+
LinkedList.Node tc1_ll1_1 = new(1);
45+
LinkedList.Node tc1_ll1_2 = new(2);
46+
LinkedList.Node tc1_ll1_3 = new(3);
47+
tc1_ll1_1.next = tc1_ll1_2;
48+
tc1_ll1_2.next = tc1_ll1_3;
49+
50+
LinkedList.Node tc1_ll2_1 = new(1);
51+
tc1_ll2_1.next = tc1_ll1_3;
52+
53+
tests = [
54+
new()
55+
{
56+
title = "Sample Test case 0",
57+
llist1 = tc0_ll1_1,
58+
llist2 = tc0_ll2_1,
59+
expected = 2
60+
},
61+
new()
62+
{
63+
title = "Sample Test case 1",
64+
llist1 = tc1_ll1_1,
65+
llist2 = tc1_ll2_1,
66+
expected = 3
67+
}
68+
];
69+
}
70+
71+
[TestMethod]
72+
public void testLinkedListCycle()
73+
{
74+
int? result;
75+
76+
foreach (FindMergeNodeTestCase test in tests)
77+
{
78+
result = FindMergeNode.findMergeNode(test.llist1, test.llist2);
79+
Assert.AreEqual(
80+
test.expected,
81+
result,
82+
String.Format(
83+
"{0} findMergeNode({1}, {2}) => must be: {3}",
84+
test.title,
85+
test.llist1,
86+
test.llist2,
87+
test.expected
88+
)
89+
);
90+
}
91+
}
92+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// @link Problem definition [[docs/hackerrank/interview_preparation_kit/linked_lists/find-the-merge-point-of-two-joined-linked-lists.md]]
2+
3+
namespace algorithm_exercises_csharp.hackerrank.interview_preparation_kit;
4+
5+
using System.Diagnostics.CodeAnalysis;
6+
7+
public class FindMergeNode
8+
{
9+
[ExcludeFromCodeCoverage]
10+
protected FindMergeNode() { }
11+
12+
public static int? findMergeNode(LinkedList.Node head1, LinkedList.Node head2)
13+
{
14+
List<LinkedList.Node> llindex = [];
15+
LinkedList.Node? node = head1;
16+
int? result = null;
17+
18+
while (node != null)
19+
{
20+
llindex.Add(node);
21+
node = node.next;
22+
}
23+
24+
node = head2;
25+
bool resume = true;
26+
while (node != null && resume)
27+
{
28+
if (llindex.Contains(node))
29+
{
30+
result = node.data;
31+
resume = false;
32+
}
33+
llindex.Add(node);
34+
node = node.next;
35+
}
36+
37+
return result;
38+
}
39+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# [Linked Lists: Find Merge Point of Two Lists](https://www.hackerrank.com/challenges/find-the-merge-point-of-two-joined-linked-lists)
2+
3+
- Difficulty: `#easy`
4+
- Category: `#ProblemSolvingIntermediate`
5+
6+
Given pointers to the head nodes of linked lists that merge together
7+
at some point, find the node where the two lists merge.
8+
The merge point is where both lists point to the same node,
9+
i.e. they reference the same memory location.
10+
It is guaranteed that the two head nodes will be different,
11+
and neither will be NULL.
12+
If the lists share a common node, return that node's value.
13+
14+
**Note**: After the merge point, both lists will share the same node pointers.
15+
16+
## Example
17+
18+
In the diagram below, the two lists converge at Node x:
19+
20+
```text
21+
[List #1] a--->b--->c
22+
\
23+
x--->y--->z--->NULL
24+
/
25+
[List #2] p--->q
26+
```
27+
28+
## Function Description
29+
30+
Complete the findMergeNode function in the editor below.
31+
32+
findMergeNode has the following parameters:
33+
34+
- SinglyLinkedListNode pointer head1: a reference to the head of the first list
35+
- SinglyLinkedListNode pointer head2: a reference to the head of the second list
36+
37+
## Returns
38+
39+
- int: the `data` value of the node where the lists merge
40+
41+
## Input Format
42+
43+
Do not read any input from stdin/console.
44+
45+
The first line contains an integer `t`, the number of test cases.
46+
47+
Each of the test cases is in the following format:
48+
The first line contains an integer, `index`,
49+
the node number where the merge will occur.
50+
51+
The next line contains an integer,
52+
`list1count` that is the number of nodes in the first list.
53+
Each of the following `list1count` lines contains a `data` value for a node.
54+
The next line contains an integer,
55+
`list2count` that is the number of nodes in the second list.
56+
Each of the following lines contains a `data` value for a node.
57+
58+
## Constraints
59+
60+
The lists will merge.
61+
62+
`head1, head2 != null`
63+
64+
`head1 != head2`
65+
66+
## Sample Input
67+
68+
The diagrams below are graphical representations of the
69+
lists that input nodes `head1` and `head2` are connected to.
70+
71+
## Test Case 0
72+
73+
```text
74+
1
75+
\
76+
2--->3--->NULL
77+
/
78+
1
79+
```
80+
81+
## Test Case 1
82+
83+
```text
84+
1--->2
85+
\
86+
3--->Null
87+
/
88+
1
89+
```
90+
91+
## Sample Output
92+
93+
```text
94+
2
95+
3
96+
```
97+
98+
## Explanation
99+
100+
Test Case 0: As demonstrated in the diagram above,
101+
the merge node's data field contains the integer `2`.
102+
Test Case 1: As demonstrated in the diagram above,
103+
the merge node's data field contains the integer `3`.

0 commit comments

Comments
 (0)