Two Linked List Intersection

easy
1. You are given two singly linked list with N and M nodes respectively.
  2. You have to write a function that returns the head of the intersection list
     of two linked lists.
  3. display is a utility function which displays the contents of Linked List,
     feel free to use it for debugging purposes.
  4. main takes input from the users and creates the Linked Lists. You can use
     display to know its contents.
  5. This is a functional problem. 
  6. You should code only the findIntersection function. It takes as input the
     heads of the first and second linked list respectively. It should create a new intersection linked list of the two linked lists and return the head of the new
     list.
     Note: The new list formed should be in non-decreasing order. Also if there is no intersecting nodes among two linked lists, then print empty line.
  7. Don't change the code of Node, main and display.

Input Format

First line takes N, the number of elements in the first list. Second line takes input N space separated numbers reperesenting elements of the first linked list. Third line takes M, the number of elements in the second list. Fourth line takes input M space separated numbers reperesenting elements of the second linked list. Input is handled for you.

Output Format

Intersection linked list Output is handled for you.

Constraints

1 <= N <= 1000
  1 <= M <= 1000

Notice

Try First, Check Solution later

1. You should first read the question and watch the question video.
2. Think of a solution approach, then try and submit the question on editor tab.
3. We strongly advise you to watch the solution video for prescribed approach.

Example

Input
6
9 6 4 2 3 8
4
1 2 8 6
Output
2 6 8
Previous
Tiny Big Tyny
Next
Add One To Linked List

Related Questions