Challenge 9: Union and Intersection of Lists Using Hashing
Given the two lists, find the union and intersection of their elements using hashing. Solve this exercise and see if your output matches the expected output.
We'll cover the following
Problem statement
Union and intersection are two of the most popular operations, which can be performed on data sets. Now, you will be implementing them for linked lists! Let’s take a look at their definitions.
Union
Given the two lists of A and B, the union is the list that contains elements or objects that belong to either A or to B or to both, but duplicates are not allowed.
Intersection
Given the two lists of **A** and **B**, the intersection is the largest list, which contains all the elements that are common to both the sets, but duplicates are not allowed.The Union( LinkedList list1, LinkedList list2)
function takes two linked lists and returns their union.
The Intersection( LinkedList list1, LinkedList list2)
function takes two linked lists and returns all the elements that are common between two linked lists.
Input
This is two linked lists.
Output
- A list containing the union of the two lists
- A list containing the intersection of the two lists
Sample input
list1 = 10->20->80->20->60->null
list2 = 15->20->30->60->60->45->null
Sample output
Union = 10->20->80->60->15->30->45->null
Intersection = 20->60->null
Level up your interview prep. Join Educative to access 70+ hands-on prep courses.