VOOZH about

URL: https://www.geeksforgeeks.org/dsa/java-program-for-union-and-intersection-of-two-linked-lists/

⇱ Java Program For Union And Intersection Of Two Linked Lists - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java Program For Union And Intersection Of Two Linked Lists

Last Updated : 23 Jul, 2025

Write a java program for a 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

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 a new list ans and store first and second list data to set to remove duplicate data
and then store it into our new list ans and return its head.

Below is the implementation of above approach:

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.


Below is the implementation of above approach:


Output
10-->20-->30-->40-->50-->60-->70-->80-->90-->None


Method 3 (Use Hashing):


1. 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.
2. 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.

Below is the implementation of above approach:

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.

Please refer complete article on Union and Intersection of two Linked Lists for more details!

Comment