# Union and Intersection of two linked lists | Set-2 (Using Merge Sort)

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.

Examples:

```Input:
List1: 10 -> 15 -> 4 ->20
lsit2:  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.

There were three methods discussed in this post with an implementation of Method 1.
In this post, we will see an implementation of Method 2 i.e. Using Merge sort.

```Implementation:
Following are the steps to be followed to get union and intersection lists.

1) Sort both Linked Lists using merge sort.
This step takes O(mLogm) time.

2) Linearly scan both sorted lists to get the union and intersection.
This step takes O(m + n) time.
```

Just like Method 1, This method also assumes that there are distinct elements in the lists.

```// C++ program to find union and intersection of
// two unsorted linked lists in O(n Log n) time.
#include<iostream>
using namespace std;

struct Node
{
int data;
struct Node* next;
};

/* A utility function to insert a node at the begining
void push(struct Node** head_ref, int new_data)
{
/* allocate node */
struct Node* new_node =
(struct Node*) malloc(sizeof(struct Node));

/* put in the data */
new_node->data = new_data;

/* link the old list off the new node */

/* move the head to point to the new node */
}

/* UTILITY FUNCTIONS */
/* Split the nodes of the given list into front and back halves,
and return the two lists using the reference parameters.
If the length is odd, the extra node should go in the front list.
Uses the fast/slow pointer strategy. */
void FrontBackSplit(struct Node* source, struct Node** frontRef,
struct Node** backRef)
{
struct Node* fast;
struct Node* slow;
if (source==NULL || source->next==NULL)
{
/* length < 2 cases */
*frontRef = source;
*backRef = NULL;
}
else
{
slow = source;
fast = source->next;

while (fast != NULL)
{
fast = fast->next;
if (fast != NULL)
{
slow = slow->next;
fast = fast->next;
}
}

/* 'slow' is before the midpoint in the list,
so split it in two at that point. */
*frontRef = source;
*backRef = slow->next;
slow->next = NULL;
}
}

/* See http://www.geeksforgeeks.org/?p=3622 for details
of this function */
struct Node* SortedMerge(struct Node* a, struct Node* b)
{
struct Node* result = NULL;

/* Base cases */
if (a == NULL)
return(b);
else if (b==NULL)
return(a);

/* Pick either a or b, and recur */
if (a->data <= b->data)
{
result = a;
result->next = SortedMerge(a->next, b);
}
else
{
result = b;
result->next = SortedMerge(a, b->next);
}
return(result);
}

/* sorts the linked list by changing next pointers
(not data) */
{
struct Node *a, *b;

/* Base case -- length 0 or 1 */
return;

/* Split head into 'a' and 'b' sublists */

/* Recursively sort the sublists */
mergeSort(&a);
mergeSort(&b);

/* answer = merge the two sorted lists together */
}

{
struct Node *result = NULL;

// Traverse both lists and store the  element in
// the resu1tant list
while (t1 != NULL && t2 != NULL)
{
// Move to the next of first list
// if its element is smaller
if (t1->data < t2->data)
{
push(&result, t1->data);
t1 = t1->next;
}

// Else move to the next of second list
else if (t1->data>t2->data)
{
push(&result, t2->data);
t2 = t2->next;
}

// If same then move to the next node
// in both lists
else
{
push(&result, t2->data);
t1 = t1->next;
t2 = t2->next;
}
}

/* Print remaining elements of the lists */
while (t1 != NULL)
{
push(&result, t1->data);
t1 = t1->next;
}
while (t2 != NULL)
{
push(&result, t2->data);
t2 = t2->next;
}

return result;
}

/* Function to get intersection of two linked lists
{
struct Node *result = NULL;

// Traverse both lists and store the same element
// in the resu1tant list
while (t1 != NULL && t2 != NULL)
{
// Move to the next of first list if smaller
if (t1->data < t2->data)
t1 = t1->next;

// Move to the next of second list if it is smaller
else if (t1->data > t2->data)
t2 = t2->next;

// If both are same
else
{
// Store current element in the list
push(&result, t2->data);

// Move to the next node of both lists
t1 = t1->next;
t2 = t2->next;
}
}

//return the resultant list
return result;
}

/* A utility function to print a linked list*/
void printList (struct Node *node)
{
while (node != NULL)
{
printf ("%d ", node->data);
node = node->next;
}
}

/* Drier program to test above function*/
int main()
{
struct Node* intersection_list = NULL;
struct Node* union_list = NULL;

/*create a linked lits 11->10->15->4->20 */

/*create a linked lits 8->4->2->10 */

/* Sort the above created Linked List */

printf("First list is \n");

printf("\nSecond list is \n");

printf("\nIntersection list is \n");
printList(intersection_list);

printf("\nUnion list is \n");
printList(union_list);

return 0;
}
```

Output:

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

Time complexity of this method is O(m Log m + n Log n).

In the next post, Method-3 will be discussed i.e. using hashing.

### Asked in: Amazon, Flipkart, Komli Media, Microsoft, Taxi4Sure, [24]7 Innovation Lab

This article is contributed by Sahil Chhabra. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

# GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
2.7 Average Difficulty : 2.7/5.0
Based on 4 vote(s)