Javatpoint Logo
Javatpoint Logo

Union and Intersection of two Linked Lists

Make union and intersection lists containing the union and intersection of the elements present in the two specified Linked Lists. It is irrelevant how the elements are arranged in output lists.

EXAMPLES

Example-1

Output:

Intersection List: 4->10
Union List: 2->8->20->4->15->10 

Method1: Simple

CODE

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

Analysis of Complexity:

Time Complexity:

O(m*n) time complexity.

Here, "m" and "n" refer to the first and second lists' respective total numbers of elements.

For the union, we determine whether each element in list-2 is already present in the resultant list created using list-1.

For intersection, we determine whether each element in list-1 is also present in list-2.

Additional Space: O (1).

No data structure is used to store values.

Method 2: Use Merge Sort

The methods used in this method for intersection and union are fairly similar. We traverse the sorted lists to obtain union and intersection after sorting the provided lists.

The actions to take in order to obtain intersection and union lists are as follows.

Merge sort is used to order the first linked list. It takes this step O(mLogm) time. For information on this phase, see this post.

Merge sort is used to order the second Linked List. It takes this step O(nLogn) time. For information on this phase, see this post.

Using a linear search, find the intersection and union of the two sorted lists. It takes this step O(m + n) time. The same algorithm that is used to sort arrays can be applied to this phase.

This approach's time complexity is O(mLogm + nLogn), which is less time-consuming than method 1's.

Method 3: Hashing

Union (list1, list2)

Make a hash table that is empty and initialise the result list to be NULL. Look at the element in the hash table for each element you visit as you go through both lists one at a time. In the event that the element is missing, add it to the result list. Ignore the element if it is present.

Intersection (list1, list2)

Make a hash table that is empty and initialise the result list to be NULL. List traversal 1. Insert the entry in the hash table for each element in list1 that is being examined. Look up each element in the hash table as you go through list 2, one element at a time. Insert the element into the result list if it is present. Ignore the element if it's not there.

The two techniques mentioned above presuppose that there are no duplicates.

CODE

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

Analysis of Complexity:

Time Complexity:

O(m+n) time complexity.

Here, "m" and "n" refer to the first and second lists' respective total numbers of elements.

Union requires traversing both lists, storing the elements in hash maps, and updating each count.

For a crossroads: List-1 should be traversed, its elements should be stored in a hash map, and then each element in list-2 should be checked to see if it already exists in the map. There is an O(1) time delay.

For intersection, we determine whether each element in list-1 is also present in list-2.

Additional Space: O (m+n).

Hash-map data structures are used to store values.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA