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.

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
Union of two linked list

Union

intersection of two linked list

Intersection

Method 1 (Simple):

Following are simple algorithms to get union and intersection lists respectively.

Intersection (list1, list2):
Initialize result list as NULL. Traverse list1 and look for its each element in list2, if the element is present in list2, then add the element to result.

Union (list1, list2):
Initialize 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 result then do not insert it to result, otherwise insert.

[ad type=”banner”]

This method assumes that there are no duplicates in the given lists.

[pastacode lang=”java” manual=”%0Aclass%20LinkedList%0A%7B%0A%20%20%20%20Node%20head%3B%20%20%0A%20%0A%20%20%20%20class%20Node%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20int%20data%3B%0A%20%20%20%20%20%20%20%20Node%20next%3B%0A%20%20%20%20%20%20%20%20Node(int%20d)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20data%20%3D%20d%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20next%20%3D%20null%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%0A%20%20%20%20void%20getUnion(Node%20head1%2C%20Node%20head2)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20Node%20t1%20%3D%20head1%2C%20t2%20%3D%20head2%3B%0A%20%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20while%20(t1%20!%3D%20null)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20push(t1.data)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20t1%20%3D%20t1.next%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20while%20(t2%20!%3D%20null)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(!isPresent(head%2C%20t2.data))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20push(t2.data)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20t2%20%3D%20t2.next%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20void%20getIntersection(Node%20head1%2C%20Node%20head2)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20Node%20result%20%3D%20null%3B%0A%20%20%20%20%20%20%20%20Node%20t1%20%3D%20head1%3B%0A%20%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20while%20(t1%20!%3D%20null)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(isPresent(head2%2C%20t1.data))%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20push(t1.data)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20t1%20%3D%20t1.next%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%0A%20%20%20%20void%20printList()%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20Node%20temp%20%3D%20head%3B%0A%20%20%20%20%20%20%20%20while(temp%20!%3D%20null)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.print(temp.data%2B%22%20%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20temp%20%3D%20temp.next%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20System.out.println()%3B%0A%20%20%20%20%7D%0A%20%0A%20%0A%20%20%20%0A%20%20%20%20void%20push(int%20new_data)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20Node%20new_node%20%3D%20new%20Node(new_data)%3B%0A%20%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20new_node.next%20%3D%20head%3B%0A%20%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20head%20%3D%20new_node%3B%0A%20%20%20%20%7D%0A%20%0A%20%0A%20%20%20%0A%20%20%20%20boolean%20isPresent%20(Node%20head%2C%20int%20data)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20Node%20t%20%3D%20head%3B%0A%20%20%20%20%20%20%20%20while%20(t%20!%3D%20null)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(t.data%20%3D%3D%20data)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20true%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20t%20%3D%20t.next%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20return%20false%3B%0A%20%20%20%20%7D%0A%20%0A%20%0A%20%20%20%20%0A%20%20%20%20public%20static%20void%20main(String%20args%5B%5D)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20LinkedList%20llist1%20%3D%20new%20LinkedList()%3B%0A%20%20%20%20%20%20%20%20LinkedList%20llist2%20%3D%20new%20LinkedList()%3B%0A%20%20%20%20%20%20%20%20LinkedList%20unin%20%3D%20new%20LinkedList()%3B%0A%20%20%20%20%20%20%20%20LinkedList%20intersecn%20%3D%20new%20LinkedList()%3B%0A%20%0A%20%20%20%20%20%0A%20%20%20%20%20%20%20%20llist1.push(20)%3B%0A%20%20%20%20%20%20%20%20llist1.push(4)%3B%0A%20%20%20%20%20%20%20%20llist1.push(15)%3B%0A%20%20%20%20%20%20%20%20llist1.push(10)%3B%0A%20%0A%0A%20%20%20%20%20%20%20%20llist2.push(10)%3B%0A%20%20%20%20%20%20%20%20llist2.push(2)%3B%0A%20%20%20%20%20%20%20%20llist2.push(4)%3B%0A%20%20%20%20%20%20%20%20llist2.push(8)%3B%0A%20%0A%20%20%20%20%20%20%20%20intersecn.getIntersection(llist1.head%2C%20llist2.head)%3B%0A%20%20%20%20%20%20%20%20unin.getUnion(llist1.head%2C%20llist2.head)%3B%0A%20%0A%20%20%20%20%20%20%20%20System.out.println(%22First%20List%20is%22)%3B%0A%20%20%20%20%20%20%20%20llist1.printList()%3B%0A%20%0A%20%20%20%20%20%20%20%20System.out.println(%22Second%20List%20is%22)%3B%0A%20%20%20%20%20%20%20%20llist2.printList()%3B%0A%20%0A%20%20%20%20%20%20%20%20System.out.println(%22Intersection%20List%20is%22)%3B%0A%20%20%20%20%20%20%20%20intersecn.printList()%3B%0A%20%0A%20%20%20%20%20%20%20%20System.out.println(%22Union%20List%20is%22)%3B%0A%20%20%20%20%20%20%20%20unin.printList()%3B%0A%20%20%20%20%7D%0A%7D” message=”Java Program” highlight=”” provider=”manual”/]

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

Time Complexity: O(mn) for both union and intersection operations. Here m is the number of elements in first list and n is the number of elements in second list.

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.
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.
2) Sort the second Linked List using merge sort. This step takes O(nLogn) time.
3) Linearly scan both sorted lists to get the union and intersection. This step takes O(m + n) time.

Time complexity of this method is O(mLogm + nLogn) which is better than method 1’s time complexity.

[ad type=”banner”]

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 the element in hash table. If the element is not present, then insert the element to 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 hash table. Traverse list2, for each element being visited in list2, look the element in hash table. If the element is present, then insert the element to result list. If the element is not present, then ignore it.

Both of the above methods assume that there are no duplicates.

Time complexity of this method depends on the hashing technique used and the distribution of elements in input lists. In practical, this approach may turn out to be better than above 2 methods.

[ad type=”banner”]

Categorized in: