Linked List Problems in Java - 60: Intersection of two Linked Lists using Hashing

preview_player
Показать описание

To intersection of two linked list, using Hashset, we merge only nodes from both linked list.
First iterate node from 1st linked list & store in hashset, then iterate from 2nd linked list & link only nodes, which are present in hashset.
Time complexity will be O(n). Do watch video for more info.

CHECK OUT CODING SIMPLIFIED

★☆★ VIEW THE BLOG POST: ★☆★

I started my YouTube channel, Coding Simplified, during Dec of 2015.
Since then, I've published over 200+ videos.

★☆★ SUBSCRIBE TO ME ON YOUTUBE: ★☆★

★☆★ Send us mail at: ★☆★

This problem is similar to following:
intersection of two linked list,
how to intersection of two linked list,
intersection of two linked list using Hashing,
intersection,
Coding Simplified,
Linked list problems,
Linked list,
java
Рекомендации по теме
Комментарии
Автор

Thanks for this!! Had no idea what was wrong with my code

afridimajeed
Автор

Sir if we have duplicate in first list then Hashset take care of that, but while checking from second list if there is duplicate in second list it will add duplicates in final list because condition if(s1.contains(node2.data)) will always be true for duplicates on second list.
Why not use second Hashset s2 for storing elements that we have already added in our final linked list and checking if(s1.contains(node2.data) && !s2.contains(node2.data) ).

Or remove elements from Hashset s after we added element in our final list.

mohitkumar-zcwd