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

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

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.
.
 .

Sample Input

The diagrams below are graphical representations of the lists that input nodes  and  are connected to.

Test Case 0

1 \ 2--->3--->NULL / 1

Test Case 1

1--->2 \ 3--->Null / 1

Sample Output

2 3

Explanation

Test Case 0: As demonstrated in the diagram above, the merge node's data field contains the integer .
Test Case 1: As demonstrated in the diagram above, the merge node's data field contains the integer .

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Operations of Linked List
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-engineering and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY