Given pointers to the head nodes of linked lists that merge together at some point, find the Node where the two lists merge. It is guaranteed that the two head Nodes will be different, and neither will be NULL.
In the diagram below, the two lists converge at Node x:
Complete the int FindMergeNode(Node* headA, Node* headB) method so that it finds and returns the data value of the Node where the two lists merge.
The FindMergeNode(Node*,Node*) method has two parameters, and , which are the non-null head Nodes of two separate linked lists that are guaranteed to converge. Do not read any input from stdin/console.
Each Node has a data field containing an integer; return the integer data for the Node where the two lists converge. Do not write any output to stdout/console.
The diagrams below are graphical representations of the lists that input Nodes and are connected to. Recall that this is a method-only challenge; the method only has initial visibility to those Nodes and must explore the rest of the Nodes using some algorithm of your own design.
Test Case 0
Test Case 1
Test Case 0: As demonstrated in the diagram above, the merge Node's data field contains the integer (so our method should return ). Test Case 1: As demonstrated in the diagram above, the merge Node's data field contains the integer (so our method should return ).