# Find common elements in three linked lists

Given three linked lists, find all common element among the three linked lists.

Examples:

Input : 10 15 20 25 12 10 12 13 15 10 12 15 24 25 26 Output : 10 12 15 Input : 1 2 3 4 5 1 2 3 4 6 9 8 1 2 4 5 10 Output : 1 2 4

**Method 1 : (Simple) **

Use three-pointers to iterate the given three linked lists and if any element common print that element.

Time complexity of the above solution will be O(N*N*N)

**Method 2 : (Use Merge Sort) **

In this method, we first sort the three lists and then we traverse the sorted lists to get the intersection.

Following are the steps to be followed to get intersection of three 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) Sort the third Linked List using merge sort. This step takes O(pLogp) time. Refer this post for details of this step.

3) Linearly scan three sorted lists to get the intersection. This step takes O(m + n + p) time. This step can be implemented using the same algorithm as sorted arrays algorithm discussed here.

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

**Method 3 : (Hashing)**

Following are the steps to be followed to get intersection of three lists using hashing:

1) Create an empty hash table. Iterate through the first linked list and mark all the element frequency as 1 in the hash table. This step takes O(m) time.

2) Iterate through the second linked list and if current element frequency is 1 in hash table mark it as 2. This step takes O(n) time.

3) Iterate the third linked list and if the current element frequency is 2 in hash table mark it as 3. This step takes O(p) time.

4) Now iterate first linked list again to check the frequency of elements. if an element with frequency three exist in hash table, it will be present in the intersection of three linked lists. This step takes O(m) time.

Time complexity of this method is O(m + n + p) which is better than time complexity of method 1 and 2.

Below is the C++ implementation of the above idea.

`// C++ program to find common element ` `// in three unsorted linked list ` `#include <bits/stdc++.h> ` `#define max 1000000 ` `using` `namespace` `std; ` ` ` `/* Link list node */` `struct` `Node { ` ` ` `int` `data; ` ` ` `struct` `Node* next; ` `}; ` ` ` `/* A utility function to insert a node at the ` `beginning of a linked list */` `void` `push(` `struct` `Node** head_ref, ` `int` `new_data) ` `{ ` ` ` `struct` `Node* new_node = ` ` ` `(` `struct` `Node *)` `malloc` `(` `sizeof` `(` `struct` `Node)); ` ` ` `new_node->data = new_data; ` ` ` `new_node->next = (*head_ref); ` ` ` `(*head_ref) = new_node; ` `} ` ` ` `/* print the common element in between ` `given three linked list*/` `void` `Common(` `struct` `Node* head1, ` ` ` `struct` `Node* head2, ` `struct` `Node* head3) ` `{ ` ` ` ` ` `// Creating empty hash table; ` ` ` `unordered_map<` `int` `, ` `int` `> hash; ` ` ` ` ` `struct` `Node* p = head1; ` ` ` `while` `(p != NULL) { ` ` ` ` ` `// set frequency by 1 ` ` ` `hash[p->data] = 1; ` ` ` `p = p->next; ` ` ` `} ` ` ` ` ` `struct` `Node* q = head2; ` ` ` `while` `(q != NULL) { ` ` ` ` ` `// if the element is already exist in the ` ` ` `// linked list set its frequency 2 ` ` ` `if` `(hash.find(q->data) != hash.end()) ` ` ` `hash[q->data] = 2; ` ` ` `q = q->next; ` ` ` `} ` ` ` ` ` `struct` `Node* r = head3; ` ` ` `while` `(r != NULL) { ` ` ` `if` `(hash.find(r->data) != hash.end() && ` ` ` `hash[r->data] == 2) ` ` ` ` ` `// if the element frquancy is 2 it means ` ` ` `// its present in both the first and second ` ` ` `// linked list set its frquancy 3 ` ` ` `hash[r->data] = 3; ` ` ` `r = r->next; ` ` ` `} ` ` ` ` ` ` ` `for` `(` `auto` `x : hash) { ` ` ` ` ` `// if current frequency is 3 its means ` ` ` `// element is common in all the given ` ` ` `// linked list ` ` ` `if` `(x.second == 3) ` ` ` `cout << x.first << ` `" "` `; ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` ` ` `// first list ` ` ` `struct` `Node* head1 = NULL; ` ` ` `push(&head1, 20); ` ` ` `push(&head1, 5); ` ` ` `push(&head1, 15); ` ` ` `push(&head1, 10); ` ` ` ` ` `// second list ` ` ` `struct` `Node* head2 = NULL; ` ` ` `push(&head2, 10); ` ` ` `push(&head2, 20); ` ` ` `push(&head2, 15); ` ` ` `push(&head2, 8); ` ` ` ` ` `// third list ` ` ` `struct` `Node* head3 = NULL; ` ` ` `push(&head3, 10); ` ` ` `push(&head3, 2); ` ` ` `push(&head3, 15); ` ` ` `push(&head3, 20); ` ` ` ` ` `Common(head1, head2, head3); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

10 15 20

Time Complexity : O(m + n + p)

## Recommended Posts:

- Find count of common nodes in two Doubly Linked Lists
- Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes
- First common element in two linked lists
- Longest common suffix of two linked lists
- Minimum Index Sum for Common Elements of Two Lists
- Find the common nodes in two singly linked list
- Find a triplet from three linked lists with sum equal to a given number
- Find distinct elements common to all rows of a matrix
- Find smallest range containing elements from k lists
- Create a linked list from two linked lists by choosing max element at each position
- Count items common to both the lists but with different prices
- Find unique elements in linked list
- Find smallest and largest elements in singly linked list
- Find minimum and maximum elements in singly Circular Linked List
- Identical Linked Lists

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.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.