
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find common elements in three linked lists in C++
Suppose we have three linked lists. We have to find all the common elements that are present in these three linked lists. Suppose these lists are [10, 12, 15, 20, 25], [10, 12, 13, 15] and [10, 12, 15, 24, 25, 26], then the common elements in these three lists are 10, 12 and 15.
We will use the hashing technique to solve this problem. To solve this, we have to follow these steps −
Create an empty hash table, and go through each element in the first table, and insert the elements, and mark the frequency as 1
Iterate through the second linked list, then if the current frequency is 1 for the element, then make it 2
Iterate through the third linked list, then if the current frequency is 2 for the element, then make it 3
Now iterate through the first list again to check the frequency of elements, if there is some element whose frequency is 3, then print that element, and go for next
Example
#include<iostream> #include<cmath> #include<unordered_map> using namespace std; class Node { public: int data; Node* next; }; void addNode(Node** start, int data) { Node* newNode = new Node; newNode->data = data; newNode->next = (*start); (*start) = newNode; } void findCommonValues(Node* list1, Node* list2, Node* list3) { unordered_map<int, int> hash; Node* p = list1; while (p != NULL) { hash[p->data] = 1; p = p->next; } Node* q = list2; while (q != NULL) { if (hash.find(q->data) != hash.end()) hash[q->data] = 2; q = q->next; } Node* r = list3; 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() { Node* list1 = NULL; addNode(&list1, 10); addNode(&list1, 12); addNode(&list1, 15); addNode(&list1, 20); addNode(&list1, 25); Node* list2 = NULL; addNode(&list2, 10); addNode(&list2, 12); addNode(&list2, 13); addNode(&list2, 15); Node* list3 = NULL; addNode(&list3, 10); addNode(&list3, 12); addNode(&list3, 15); addNode(&list3, 24); addNode(&list3, 25); addNode(&list3, 26); cout << "Common elements are: "; findCommonValues(list1, list2, list3); }
Output
Common elements are: 10 12 15
- Related Articles
- Python program to find common elements in three lists using sets
- Find common elements in list of lists in Python
- Find count of common nodes in two Doubly Linked Lists in C++
- Find common elements in three sorted arrays in C++
- Java program to find common elements in three sorted arrays
- Python program to find common elements in three sorted arrays?
- C# program to find common elements in three sorted arrays
- C# program to find common elements in three arrays using sets
- Find common elements in three sorted arrays by dictionary intersection in Python
- Python Program to Find the first Common Element between the 2 given Linked Lists
- Program to interleave list elements from two linked lists in Python
- Find a triplet from three linked lists with a sum equal to a given number in C++
- Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes in Python
- Minimum Index Sum for Common Elements of Two Lists in C++
- Doubly linked lists in Javascript
