Given three linked lists, find all common element among the three linked lists.
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 implementation of the above idea.
10 15 20
Time Complexity : O(m + n + p)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes
- Find a triplet from three linked lists with sum equal to a given number
- Find count of common nodes in two Doubly Linked Lists
- Longest common suffix of two linked lists
- Remove the common nodes in two Singly Linked Lists
- First common element in two linked lists
- Find three element from different three arrays such that a + b + c = sum
- Minimum Index Sum for Common Elements of Two Lists
- Number of quadruples where the first three terms are in AP and last three terms are in GP
- Create a linked list from two linked lists by choosing max element at each position
- Count of lists which are not a subset of any other given lists
- Count items common to both the lists but with different prices
- Find intersection point of two Linked Lists without finding the length
- Find the common nodes in two singly linked list
- Sum of nodes of Linked List whose contains values with exactly three factors
- Merge Sort for Linked Lists
- Compare two strings represented as linked lists
- Merge two sorted linked lists such that merged list is in reverse order
- In-place Merge two linked lists without changing links of first list
- Subtract Two Numbers represented as 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 firstname.lastname@example.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.