Intersection of Two Linked Lists (LeetCode 160) | Full solution with animations | Study Algorithms

preview_player
Показать описание
A linked list node has 2 dimensions. One is the value of the node and the other is the address. This information is really helpful when solving questions like finding the intersection point of two linked lists. This video explores 2 solutions, where we take advantage of the address of a node and uses a mathematical trick to optimize it. All along with step by step demonstrations and a dry-run of code in JAVA.

Chapters:
00:00 - Intro
01:10 - Problem statement and description
03:15 - Brute Force approach to find the intersection point
06:54 - Time optimized approach to find the intersection
10:07 - Space optimized approach to find the intersection
13:00 - Dry-run of code
15:36 - Final Thoughts

📚 Links to topics I talk about in the video:

📖 Reference Books:

🎥 My Recording Gear:

💻 Get Social 💻

#leetcode #programming #linkedlists
Рекомендации по теме
Комментарии
Автор

There are many solution videos available on youtube but ur explanation is the best ever.

adhirajkar
Автор

wow understood all the approaches . Thank you😌

nandiniambajipeta
Автор

Thanks brother. You are helping us by the best explanations I have have ever seen... Thanks a lot. Keep working and keep helping... Now I am your new subscriber

rishabhupadhyay
Автор

Your way of Explaining solution is awesome sir

Parth-zk
Автор

Thankyou Nikhil, Your animation explanations are a revelation..wish I'd found your channel sooner!

Mania_Dev
Автор

Nicely Explained and good use of animations.

ramsidh
Автор

i am shocked and amazed by this solution

parthanuj
Автор

In every problem "Final thoughts" are awesome....

sammedpatil
Автор

thank you sir ... We like the way you explain

ishikagoyal
Автор

Thank you sir for explaining everything thoroughly ❤

vineetkumar
Автор

First problem I solved without looking at solution🥰

sachinsoma
Автор

Thanks for your multiple examples.

1) In 3rd example. Small correction. Length of the list 1 is 9 but not 10. So difference is 3 and need to advance to 3 nodes as "head 1" is already at first node.

2) As part of 2nd example, first list is kept in separate array / HashSet. You mentioned second list elements address (M, N, O...) to compare with the address in array / HashSet in O(1). How is this possible. Please let me know if there is any such example for Hash table for linked list.

beemavishnu
Автор

sorry if i'm wrong, isn;t l1 length 9?

Lakshareads
Автор

Sir you are amazing
I love your way of explaining ❤
Sir how can I connect you personally?

Sulthanul_Faris
Автор

qq, is the TC for the first brute force approach you explained 0(m*n)?

Babas
Автор

node* intersectionY(node* head1, node* head2) {
if (head1 == NULL || head2 == NULL) return NULL;

node* temp = head1;
map<node*, int> mpp;

// Insert nodes of the first linked list into the map
while (temp != NULL) {
mpp[temp]=1;
temp = temp->next;
}
// Traverse the second linked list and check for intersection
temp = head2;
while (temp != NULL) {
if (mpp.find(temp) != mpp.end())
{
return temp;
}
cout<<temp->data<<" ";
temp = temp->next;
}
return NULL;
}int main()
{
vector<int> arr={3, 1, 4, 6, 2};
vector<int> b={1, 2, 4, 5, 4, 6, 2};
node *head1=convertarrtoLL(arr);
node* head2=convertarrtoLL(b);
node* head=intersectionY(head1, head2);
if (head != nullptr) {
cout << "Intersection found at value: " << head->data << endl;
} else {
cout << "No intersection found." << endl;
}


}1 2 4 5 4 6 2 No intersection found.?? pls tell me why its not coming??imtrying it from past 4 hrs still i can't pinpoint my mistake?

kale-lbpr
Автор

bhaiya i think the length of the linked list 1 is 9 , other than that thank you so much for the solution.

udayrajvadeghar
Автор

I got an error in getListLength(), cannot find symbol is shown
What can i do sir

mahatrt
Автор

what if we have same value before intersection how will that situation be taken care of ways Great Explanation

RohitKumar-ltg
Автор

Bro, getListLength is user-defined fn or inbuilt fn?

az-opoc