Given two Linked Lists, create union and intersection lists that contain union and intersection of the elements present in the given lists. Order of elements in output lists doesn’t matter.
Input: List1: 10 -> 15 -> 4 -> 20 List2: 8 -> 4 -> 2 -> 10 Output: Intersection List: 4 -> 10 Union List: 2 -> 8 -> 20 -> 4 -> 15 -> 10 Explanation: In this two lists 4 and 10 nodes are common. The union lists contains all the nodes of both the lists. Input: List1: 1 -> 2 -> 3 -> 4 List2: 3 -> 4 -> 8 -> 10 Output: Intersection List: 3 -> 4 Union List: 1 -> 2 -> 3 -> 4 -> 8 -> 10 Explanation: In this two lists 4 and 3 nodes are common. The union lists contains all the nodes of both the lists.
We have already discussed Method-1 and Method-2 of this question.
In this post, its Method-3 (Using Hashing) is discussed with a Time Complexity of O(m+n) i.e. better than both methods discussed earlier.
Implementation: 1- Start traversing both the lists. a) Store the current element of both lists with its occurrence in the map. 2- For Union: Store all the elements of the map in the resultant list. 3- For Intersection: Store all the elements only with an occurrence of 2 as 2 denotes that they are present in both the lists.
Below is the C++ implementation of the above steps.
First list is 5 4 3 2 1 Second list is 6 5 3 1 Intersection list is 3 5 1 Union list is 3 4 6 5 2 1
We can also handle the case of duplicates by maintaining separate Hash for both the lists.
- Time Complexity: O(m+n).
Here ‘m’ and ‘n’ are number of elements present in first and second lists respectively.
For Union: Traverse both the lists, store the elements in Hash-map and update the respective count.
For Intersection: Check if count of an element in hash-map is ‘2’.
- Auxiliary Space: O(m+n).
Use of Hash-map data structure for storing values.
This article is contributed by Sahil Chhabra. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Union and Intersection of two Linked Lists
- Union and Intersection of two linked lists | Set-2 (Using Merge Sort)
- VMware Interview Experience | Set3 (On-Campus for Full Time and Internship Offers)
- Find Union and Intersection of two unsorted arrays
- Write a function to get the intersection point of two Linked Lists
- Write a function to get the intersection point of two Linked Lists | Set 2
- Find intersection point of two Linked Lists without finding the length
- Intersection of two Sorted Linked Lists
- Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes
- Create a linked list from two linked lists by choosing max element at each position
- Rearrange characters in a string such that no two adjacent are same using hashing
- Merge odd and even positioned nodes of two Linked Lists alternately
- Count of lists which are not a subset of any other given lists
- Merge two sorted linked lists
- Add two numbers represented by linked lists | Set 1
- Add two numbers represented by linked lists | Set 2
- Compare two strings represented as linked lists
- Merge two sorted linked lists such that merged list is in reverse order
- In-place Merge two linked lists without changing links of first list
- Subtract Two Numbers represented as Linked Lists