Union and Intersection of two Linked Lists

• Difficulty Level : Medium
• Last Updated : 17 Sep, 2021

Given two Linked Lists, create union and intersection lists that contain union and intersection of the elements present in the given lists. The order of elements in output lists doesn’t matter.
Example:

Input:
List1: 10->15->4->20
List2:  8->4->2->10
Output:
Intersection List: 4->10
Union List: 2->8->20->4->15->10

Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

Method 1 (Simple)
The following are simple algorithms to get union and intersection lists respectively.
Intersection (list1, list2)
Initialize the result list as NULL. Traverse list1 and look for every element in list2, if the element is present in list2, then add the element to the result.
Union (list1, list2):
Initialize the result list as NULL. Traverse list1 and add all of its elements to the result.
Traverse list2. If an element of list2 is already present in the result then do not insert it to the result, otherwise insert.
This method assumes that there are no duplicates in the given lists.
Thanks to Shekhu for suggesting this method. Following are C and Java implementations of this method.

C#

Output

First list is
10 15 4 20
Second list is
8 4 2 10
Intersection list is
4 10
Union list is
2 8 20 4 15 10

Complexity Analysis:

• Time Complexity: O(m*n).
Here ‘m’ and ‘n’ are number of elements present in the first and second lists respectively.
For union: For every element in list-2 we check if that element is already present in the resultant list made using list-1.
For intersection: For every element in list-1 we check if that element is also present in list-2.
• Auxiliary Space: O(1).
No use of any data structure for storing values.

Method 2 (Use Merge Sort)
In this method, algorithms for Union and Intersection are very similar. First, we sort the given lists, then we traverse the sorted lists to get union and intersection.
The following are the steps to be followed to get union and intersection lists.

1. Sort the first Linked List using merge sort. This step takes O(mLogm) time. Refer this post for details of this step.
2. Sort the second Linked List using merge sort. This step takes O(nLogn) time. Refer this post for details of this step.
3. Linearly scan both sorted lists to get the union and intersection. This step takes O(m + n) time. This step can be implemented using the same algorithm as sorted arrays algorithm discussed here.

The time complexity of this method is O(mLogm + nLogn) which is better than method 1’s time complexity.
Method 3 (Use Hashing)
Union (list1, list2)
Initialize the result list as NULL and create an empty hash table. Traverse both lists one by one, for each element being visited, look at the element in the hash table. If the element is not present, then insert the element into the result list. If the element is present, then ignore it.
Intersection (list1, list2)
Initialize the result list as NULL and create an empty hash table. Traverse list1. For each element being visited in list1, insert the element in the hash table. Traverse list2, for each element being visited in list2, look the element in the hash table. If the element is present, then insert the element to the result list. If the element is not present, then ignore it.
Both of the above methods assume that there are no duplicates.

C#

Output

First List is
10 15 4 20
Second List is
8 4 2 10
Intersection List is
10 4
Union List is
2 4 20 8 10 15

Complexity Analysis:

• Time Complexity: O(m+n).
Here ‘m’ and ‘n’ are number of elements present in the first and second lists respectively.
For union: Traverse both the lists, store the elements in Hash-map and update the respective count.
For intersection: First traverse list-1, store its elements in Hash-map and then for every element in list-2 check if it is already present in the map. This takes O(1) time.
• Auxiliary Space:O(m+n).
Use of Hash-map data structure for storing values.