Intersection of two sorted Linked lists
Submissions: 24836   Accuracy:

29.44%

  Difficulty: Easy   Marks: 2

Given two lists sorted in increasing order, create a new list representing the intersection of the two lists. The new list should be made with its own memory — the original lists should not be changed.

For example, let the first linked list be 1->2->3->4->6 and second linked list be 2->4->6->8, then your function should create a third list as 2->4->6.


Input:

You have to complete the method which takes 3 argument: the head of the first linked list , the head of second linked list and the head of the third link list which is to be created. You should not read any input from stdin/console. There are multiple test cases. For each test case, this method will be called individually.

Output:
Complete the Function given to produce the desired list with intersectioned values.

Constraints:
1 <=T<= 100
1 <= size of linked lists <= 100
1 <= Data in linked list nodes <= 1000

 

Example:(The input/output format below should be used for Expected Output only)

Input
1                       -->  (No of test cases)
5 4                    -->  (sizes of linked lists)
1 2 3 4 6           -->  (Elements of 1st linked list) 
2 4 6 8              -->  (Elements of 2nd linked list)
4 2
10 20 40 50
15 40

Output
2 4 6                 -->  (Elements of resultant 3rd linked list 
40

 

Note:The Input/Ouput format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.

** For More Input/Output Examples Use 'Expected Output' option **

Contributor: Amit Khandelwal
Author: shef5


If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.



Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.

to report an issue on this page.