We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Given pointers to the head nodes of linked lists that merge together at some point, find the node where the two lists merge. The merge point is where both lists point to the same node, i.e. they reference the same memory location. It is guaranteed that the two head nodes will be different, and neither will be NULL. If the lists share a common node, return that node's value.
Note: After the merge point, both lists will share the same node pointers.
Example
In the diagram below, the two lists converge at Node x:
Complete the findMergeNode function in the editor below.
findMergeNode has the following parameters:
SinglyLinkedListNode pointer head1: a reference to the head of the first list
SinglyLinkedListNode pointer head2: a reference to the head of the second list
Returns
int: the value of the node where the lists merge
Input Format
Do not read any input from stdin/console.
The first line contains an integer , the number of test cases.
Each of the test cases is in the following format:
The first line contains an integer, , the node number where the merge will occur.
The next line contains an integer, that is the number of nodes in the first list.
Each of the following lines contains a value for a node. The next line contains an integer, that is the number of nodes in the second list.
Each of the following lines contains a value for a node.
Constraints
The lists will merge.
.
.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Find Merge Point of Two Lists
You are viewing a single comment's thread. Return to all comments →
Given pointers to the head nodes of linked lists that merge together at some point, find the node where the two lists merge. The merge point is where both lists point to the same node, i.e. they reference the same memory location. It is guaranteed that the two head nodes will be different, and neither will be NULL. If the lists share a common node, return that node's value.
Note: After the merge point, both lists will share the same node pointers.
Example
In the diagram below, the two lists converge at Node x:
[List #1] a--->b--->c \ x--->y--->z--->NULL / [List #2] p--->q Function Description
Complete the findMergeNode function in the editor below.
findMergeNode has the following parameters:
SinglyLinkedListNode pointer head1: a reference to the head of the first list SinglyLinkedListNode pointer head2: a reference to the head of the second list Returns
int: the value of the node where the lists merge Input Format
Do not read any input from stdin/console.
The first line contains an integer , the number of test cases.
Each of the test cases is in the following format: The first line contains an integer, , the node number where the merge will occur. The next line contains an integer, that is the number of nodes in the first list. Each of the following lines contains a value for a node. The next line contains an integer, that is the number of nodes in the second list. Each of the following lines contains a value for a node.
Constraints
The lists will merge. . .