Union and Intersection of two Linked List using Hashing
Last Updated :
23 Jul, 2025
Given two singly Linked Lists, create union and intersection lists that contain the union and intersection of the elements present in the given lists. Each of the two linked lists contains distinct node values.
Note: The order of elements in output lists doesn’t matter.
Examples:
Input:
head1 : 10 -> 15 -> 4 -> 20
head2 : 8 -> 4 -> 2 -> 10
Output:
Intersection List: 4 -> 10
Union List: 2 -> 8 -> 20 -> 4 -> 15 -> 10
Explanation: In these two lists 4 and 10 nodes are common. The union lists contain all the nodes of both lists.
Input:
head1 : 1 -> 2 -> 3 -> 4
head2 : 3 -> 4 -> 8 -> 10
Output:
Intersection List: 3 -> 4
Union List: 1 -> 2 -> 3 -> 4 -> 8 -> 10
Explanation: In these two lists 4 and 3 nodes are common. The union lists contain all the nodes of both lists.
Approach:
We have already discussed Method-1 and Method-2 of this question. In this post, its Method-3 (Using Hashing) is discussed with a Time Complexity of O(m + n) i.e. better than both methods discussed earlier.
- Intersection Function:
- Use a Hashset to keep track of elements from the first linked list.
- Traverse the second list and check if elements are present in the Hashset.
- if present , append the elements into the result list.
- Union Function:
- Use a Hashset to collect unique elements from both lists.
- Insert elements from both lists into the Hashset .
- Append the elements from Hashset into the result list.
Below is the implementation of the above approach :
C++
// C++ code for Union and Intersection
// of two Linked List
#include <bits/stdc++.h>
using namespace std;
class Node {
public:
int data;
Node *next;
Node(int x) {
data = x;
next = nullptr;
}
};
void printList(Node *head) {
Node *curr = head;
while (curr != nullptr) {
cout << curr->data << " ";
curr = curr->next;
}
cout << endl;
}
// Function to get the intersection of two lists
Node *getIntersection(Node *head1, Node *head2) {
unordered_set<int> set;
Node *result = nullptr;
// Traverse the first list and store
// elements in a hash set
Node *p = head1;
while (p != nullptr) {
set.insert(p->data);
p = p->next;
}
// Traverse the second list and
// check for common elements
p = head2;
while (p != nullptr) {
if (set.find(p->data) != set.end()) {
// Insert at the beginning of the result list
Node *new_node = new Node(p->data);
new_node->next = result;
result = new_node;
}
p = p->next;
}
return result;
}
// Function to get the union of two lists
Node *getUnion(Node *head1, Node *head2) {
unordered_set<int> union_set;
Node *p = head1;
while (p != nullptr) {
union_set.insert(p->data);
p = p->next;
}
p = head2;
while (p != nullptr) {
union_set.insert(p->data);
p = p->next;
}
Node *result = nullptr;
Node *tail = nullptr;
// Insert all unique values into
// the result list
for (int i : union_set) {
Node *new_node = new Node(i);
if (result == nullptr) {
result = new_node;
tail = new_node;
}
else {
tail->next = new_node;
tail = tail->next;
}
}
return result;
}
// Function to print both union and intersection of two lists
void printUnionIntersection(Node *head1, Node *head2) {
Node *intersection_list = getIntersection(head1, head2);
Node *union_list = getUnion(head1, head2);
cout << "Intersection list is:" << endl;
printList(intersection_list);
cout << "Union list is:" << endl;
printList(union_list);
}
int main() {
//list1 : 1 -> 2 -> 3 -> 3 -> 4 -> 5
Node *head1 = new Node(1);
head1->next = new Node(2);
head1->next->next = new Node(3);
head1->next->next->next = new Node(3);
head1->next->next->next->next = new Node(4);
head1->next->next->next->next->next = new Node(5);
// list2 : 1 -> 5 -> 6
Node *head2 = new Node(1);
head2->next = new Node(5);
head2->next->next = new Node(6);
printUnionIntersection(head1, head2);
return 0;
}
Java
// Java program to find union and intersection
// of linked list
import java.util.HashSet;
import java.util.Set;
class Node {
int data;
Node next;
Node(int x) {
data = x;
next = null;
}
}
class GfG {
static void printList(Node head) {
Node curr = head;
while (curr != null) {
System.out.print(curr.data + " ");
curr = curr.next;
}
System.out.println();
}
// Function to get the intersection of two lists
static Node getIntersection(Node head1, Node head2) {
Set<Integer> set = new HashSet<>();
Node result = null;
// Traverse the first list and store elements in a
// hash set
Node p = head1;
while (p != null) {
set.add(p.data);
p = p.next;
}
// Traverse the second list and check for common
// elements
p = head2;
while (p != null) {
if (set.contains(p.data)) {
// Insert at the beginning of the result
// list
Node newNode = new Node(p.data);
newNode.next = result;
result = newNode;
}
p = p.next;
}
return result;
}
// Function to get the union of two lists
static Node getUnion(Node head1, Node head2) {
Set<Integer> unionSet = new HashSet<>();
Node p = head1;
while (p != null) {
unionSet.add(p.data);
p = p.next;
}
p = head2;
while (p != null) {
unionSet.add(p.data);
p = p.next;
}
Node result = null;
Node tail = null;
// Insert all unique values into the result list
for (int i : unionSet) {
Node newNode = new Node(i);
if (result == null) {
result = newNode;
tail = newNode;
}
else {
tail.next = newNode;
tail = tail.next;
}
}
return result;
}
// Function to print both union and intersection of two
// lists
static void printUnionIntersection(Node head1,Node head2) {
Node intersectionList = getIntersection(head1, head2);
Node unionList = getUnion(head1, head2);
System.out.println("Intersection list is:");
printList(intersectionList);
System.out.println("Union list is:");
printList(unionList);
}
public static void main(String[] args) {
// List 1: 1 -> 2 -> 3 -> 3 -> 4 -> 5
Node head1 = new Node(1);
head1.next = new Node(2);
head1.next.next = new Node(3);
head1.next.next.next = new Node(3);
head1.next.next.next.next = new Node(4);
head1.next.next.next.next.next = new Node(5);
// List 2: 1 -> 5 -> 6
Node head2 = new Node(1);
head2.next = new Node(5);
head2.next.next = new Node(6);
printUnionIntersection(head1, head2);
}
}
Python
# Python code for finding union and
# intersection of linkedList
class Node:
def __init__(self, x):
self.data = x
self.next = None
def print_list(head):
curr = head
while curr:
print(curr.data, end=" ")
curr = curr.next
print()
def get_intersection(head1, head2):
seen = set()
result = None
# Traverse the first list and
# store elements in a set
p = head1
while p:
seen.add(p.data)
p = p.next
# Traverse the second list and check
# for common elements
p = head2
while p:
if p.data in seen:
# Insert at the beginning of
# the result list
new_node = Node(p.data)
new_node.next = result
result = new_node
p = p.next
return result
def get_union(head1, head2):
union_set = set()
p = head1
while p:
union_set.add(p.data)
p = p.next
p = head2
while p:
union_set.add(p.data)
p = p.next
result = None
tail = None
# Insert all unique values into
# the result list
for i in union_set:
new_node = Node(i)
if result is None:
result = new_node
tail = new_node
else:
tail.next = new_node
tail = tail.next
return result
def print_union_intersection(head1, head2):
intersection_list = get_intersection(head1, head2)
union_list = get_union(head1, head2)
print("Intersection list is:")
print_list(intersection_list)
print("Union list is:")
print_list(union_list)
if __name__ == "__main__":
# List 1: 1 -> 2 -> 3 -> 3 -> 4 -> 5
head1 = Node(1)
head1.next = Node(2)
head1.next.next = Node(3)
head1.next.next.next = Node(3)
head1.next.next.next.next = Node(4)
head1.next.next.next.next.next = Node(5)
# List 2: 1 -> 5 -> 6
head2 = Node(1)
head2.next = Node(5)
head2.next.next = Node(6)
print_union_intersection(head1, head2)
C#
// C# program to find union and intersection
// of linked list
using System;
using System.Collections.Generic;
class Node {
public int Data;
public Node Next;
public Node(int x) {
Data = x;
Next = null;
}
}
class GfG {
static void PrintList(Node head) {
Node curr = head;
while (curr != null) {
Console.Write(curr.Data + " ");
curr = curr.Next;
}
Console.WriteLine();
}
// Function to get the intersection of two lists
static Node GetIntersection(Node head1, Node head2) {
HashSet<int> set = new HashSet<int>();
Node result = null;
// Traverse the first list and store elements in a
// hash set
Node p = head1;
while (p != null) {
set.Add(p.Data);
p = p.Next;
}
// Traverse the second list and check for common
// elements
p = head2;
while (p != null) {
if (set.Contains(p.Data)) {
// Insert at the beginning of the result
// list
Node newNode = new Node(p.Data);
newNode.Next = result;
result = newNode;
}
p = p.Next;
}
return result;
}
// Function to get the union of two lists
static Node GetUnion(Node head1, Node head2) {
HashSet<int> unionSet = new HashSet<int>();
Node p = head1;
while (p != null) {
unionSet.Add(p.Data);
p = p.Next;
}
p = head2;
while (p != null) {
unionSet.Add(p.Data);
p = p.Next;
}
Node result = null;
Node tail = null;
// Insert all unique values into the result list
foreach(int i in unionSet) {
Node newNode = new Node(i);
if (result == null) {
result = newNode;
tail = newNode;
}
else {
tail.Next = newNode;
tail = tail.Next;
}
}
return result;
}
// Function to print both union and intersection of two
// lists
static void PrintUnionIntersection(Node head1,Node head2) {
Node intersectionList = GetIntersection(head1, head2);
Node unionList = GetUnion(head1, head2);
Console.WriteLine("Intersection list is:");
PrintList(intersectionList);
Console.WriteLine("Union list is:");
PrintList(unionList);
}
static void Main() {
// List 1: 1 -> 2 -> 3 -> 3 -> 4 -> 5
Node head1 = new Node(1);
head1.Next = new Node(2);
head1.Next.Next = new Node(3);
head1.Next.Next.Next = new Node(3);
head1.Next.Next.Next.Next = new Node(4);
head1.Next.Next.Next.Next.Next = new Node(5);
// List 2: 1 -> 5 -> 6
Node head2 = new Node(1);
head2.Next = new Node(5);
head2.Next.Next = new Node(6);
PrintUnionIntersection(head1, head2);
}
}
JavaScript
// JavaScript program to find union and
// intersection of linked list
class Node {
constructor(x) {
this.data = x;
this.next = null;
}
}
function printList(head) {
let curr = head;
while (curr !== null) {
console.log(curr.data + " ");
curr = curr.next;
}
console.log();
}
// Function to get the intersection of two lists
function getIntersection(head1, head2) {
const set = new Set();
let result = null;
// Traverse the first list and store
// elements in a set
let p = head1;
while (p !== null) {
set.add(p.data);
p = p.next;
}
// Traverse the second list and check for common
// elements
p = head2;
while (p !== null) {
if (set.has(p.data)) {
// Insert at the beginning of the result list
const newNode = new Node(p.data);
newNode.next = result;
result = newNode;
}
p = p.next;
}
return result;
}
// Function to get the union of two lists
function getUnion(head1, head2) {
const unionSet = new Set();
let p = head1;
while (p !== null) {
unionSet.add(p.data);
p = p.next;
}
p = head2;
while (p !== null) {
unionSet.add(p.data);
p = p.next;
}
let result = null;
let tail = null;
// Insert all unique values into the result list
for (let i of unionSet) {
const newNode = new Node(i);
if (result === null) {
result = newNode;
tail = newNode;
}
else {
tail.next = newNode;
tail = tail.next;
}
}
return result;
}
// Function to print both union and intersection of two
// lists
function printUnionIntersection(head1, head2) {
const intersectionList = getIntersection(head1, head2);
const unionList = getUnion(head1, head2);
console.log("Intersection list is:");
printList(intersectionList);
console.log("Union list is:");
printList(unionList);
}
// List 1: 1 -> 2 -> 3 -> 3 -> 4 -> 5
const head1 = new Node(1);
head1.next = new Node(2);
head1.next.next = new Node(3);
head1.next.next.next = new Node(3);
head1.next.next.next.next = new Node(4);
head1.next.next.next.next.next = new Node(5);
// List 2: 1 -> 5 -> 6
const head2 = new Node(1);
head2.next = new Node(5);
head2.next.next = new Node(6);
printUnionIntersection(head1, head2);
OutputIntersection list is:
5 1
Union list is:
6 5 1 2 3 4
Time Complexity: O(m+n) , where m and n are number of elements present in first and second lists respectively.
Auxiliary Space: O(m+n)
Intersection of Two Linked Lists | DSA Practice Problem
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem