Find common elements in three linked lists
Last Updated :
09 Jan, 2023
Given three linked lists, find all common elements 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:
- Sort the first Linked List using merge sort. This step takes O(mLogm) time. Refer this post for details of this step.
- Sort the second Linked List using merge sort. This step takes O(nLogn) time. Refer this post for details of this step.
- Sort the third Linked List using merge sort. This step takes O(pLogp) time. Refer this post for details of this step.
- 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:
- 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.
- 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.
- 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.
- 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.
C++
#include <bits/stdc++.h>
#define max 1000000
using namespace std;
struct Node {
int data;
struct Node* next;
};
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;
}
void Common( struct Node* head1,
struct Node* head2, struct Node* head3)
{
unordered_map< int , int > hash;
struct Node* p = head1;
while (p != NULL) {
hash[p->data] = 1;
p = p->next;
}
struct Node* q = head2;
while (q != NULL) {
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)
hash[r->data] = 3;
r = r->next;
}
for ( auto x : hash) {
if (x.second == 3)
cout << x.first << " " ;
}
}
int main()
{
struct Node* head1 = NULL;
push(&head1, 20);
push(&head1, 5);
push(&head1, 15);
push(&head1, 10);
struct Node* head2 = NULL;
push(&head2, 10);
push(&head2, 20);
push(&head2, 15);
push(&head2, 8);
struct Node* head3 = NULL;
push(&head3, 10);
push(&head3, 2);
push(&head3, 15);
push(&head3, 20);
Common(head1, head2, head3);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int max = 1000000 ;
static class Node
{
int data;
Node next;
};
static Node push(Node head_ref,
int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
static void Common(Node head1,
Node head2,
Node head3)
{
HashMap<Integer,
Integer> hash = new HashMap<Integer,
Integer>();
Node p = head1;
while (p != null )
{
hash. put(p.data, 1 );
p = p.next;
}
Node q = head2;
while (q != null )
{
if (hash.containsKey(q.data))
hash. put(q.data, 2 );
q = q.next;
}
Node r = head3;
while (r != null )
{
if (hash.containsKey(r.data)&&
hash.get(r.data) == 2 )
hash. put(r.data, 3 );
r = r.next;
}
for (Map.Entry<Integer,
Integer> x : hash.entrySet())
{
if (x.getValue() == 3 )
System.out.println(x.getKey() + " " );
}
}
public static void main(String[] args)
{
Node head1 = null ;
head1 = push(head1, 20 );
head1 = push(head1, 5 );
head1 = push(head1, 15 );
head1 = push(head1, 10 );
Node head2 = null ;
head2 = push(head2, 10 );
head2 = push(head2, 20 );
head2 = push(head2, 15 );
head2 = push(head2, 8 );
Node head3 = null ;
head3 = push(head3, 10 );
head3 = push(head3, 2 );
head3 = push(head3, 15 );
head3 = push(head3, 20 );
Common(head1, head2, head3);
}
}
|
Python3
max = 1000000
class Node:
def __init__( self , data):
self .data = data
self . next = None
def push( head_ref, new_data):
new_node = Node(new_data)
new_node. next = head_ref
head_ref = new_node
return head_ref
def Common(head1, head2, head3):
hash = dict ()
p = head1
while (p ! = None ):
hash [p.data] = 1
p = p. next
q = head2
while (q ! = None ):
if (q.data in hash ):
hash [q.data] = 2
q = q. next
r = head3
while (r ! = None ):
if (r.data in hash ) and hash [r.data] = = 2 :
hash [r.data] = 3
r = r. next
for x in hash .keys():
if ( hash [x] = = 3 ):
print (x, end = ' ' )
if __name__ = = '__main__' :
head1 = None
head1 = push(head1, 20 )
head1 = push(head1, 5 )
head1 = push(head1, 15 )
head1 = push(head1, 10 )
head2 = None
head2 = push(head2, 10 )
head2 = push(head2, 20 )
head2 = push(head2, 15 )
head2 = push(head2, 8 )
head3 = None
head3 = push(head3, 10 )
head3 = push(head3, 2 )
head3 = push(head3, 15 )
head3 = push(head3, 20 )
Common(head1, head2, head3)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int max = 1000000;
public class Node
{
public int data;
public Node next;
};
static Node push(Node head_ref,
int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
static void Common(Node head1,
Node head2,
Node head3)
{
Dictionary< int ,
int > hash = new Dictionary< int ,
int >();
Node p = head1;
while (p != null )
{
hash.Add(p.data, 1);
p = p.next;
}
Node q = head2;
while (q != null )
{
if (hash.ContainsKey(q.data))
hash[q.data] = 2;
q = q.next;
}
Node r = head3;
while (r != null )
{
if (hash.ContainsKey(r.data)&&
hash[r.data] == 2)
hash[r.data] = 3;
r = r.next;
}
foreach (KeyValuePair< int , int > x in hash)
{
if (x.Value == 3)
Console.Write(x.Key + " " );
}
}
public static void Main(String[] args)
{
Node head1 = null ;
head1 = push(head1, 20);
head1 = push(head1, 5);
head1 = push(head1, 15);
head1 = push(head1, 10);
Node head2 = null ;
head2 = push(head2, 10);
head2 = push(head2, 20);
head2 = push(head2, 15);
head2 = push(head2, 8);
Node head3 = null ;
head3 = push(head3, 10);
head3 = push(head3, 2);
head3 = push(head3, 15);
head3 = push(head3, 20);
Common(head1, head2, head3);
}
}
|
Javascript
<script>
var max = 1000000;
class Node
{
constructor()
{
this .data = 0;
this .next = null ;
}
};
function push(head_ref, new_data)
{
var new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
function Common(head1, head2, head3)
{
var hash = new Map();
var p = head1;
while (p != null )
{
hash.set(p.data, 1);
p = p.next;
}
var q = head2;
while (q != null )
{
if (hash.has(q.data))
hash.set(q.data, 2);
q = q.next;
}
var r = head3;
while (r != null )
{
if (hash.has(r.data)&&
hash.get(r.data) == 2)
hash.set(r.data, 3);
r = r.next;
}
hash.forEach((value, key) => {
if (value == 3)
document.write(key + " " );
});
}
var head1 = null ;
head1 = push(head1, 20);
head1 = push(head1, 5);
head1 = push(head1, 15);
head1 = push(head1, 10);
var head2 = null ;
head2 = push(head2, 10);
head2 = push(head2, 20);
head2 = push(head2, 15);
head2 = push(head2, 8);
var head3 = null ;
head3 = push(head3, 10);
head3 = push(head3, 2);
head3 = push(head3, 15);
head3 = push(head3, 20);
Common(head1, head2, head3);
</script>
|
Time Complexity : O(m + n + p)
Auxiliary Space : O(m + n + p)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...