Find intersection of two linked lists

preview_player
Показать описание
Given two linked lists.Find intersection of the two linked lists.
Рекомендации по теме
Комментарии
Автор

Damn, I can't believe I haven't thought of this method. Great video.

WowPlusWow
Автор

Nice Explanation. Here is the another way without determining the length of the linked list.
1. Start iterating both the lists from the head node (pointer: list1 for first list and list2 for second list).
2.Interchange the pointers if any of the list is terminated

For eg:
list1: 1->2>3->10->11
list2: 5->10->11
ptr1=list1
ptr2 = list2
after three iterations,
ptr2 will point to head of list1 : 1->2->3->10->11 while the ptr1 pointer is still point to 10 of the list1
after 3 more iterations,
ptr1 is pointing to 10(list2) and ptr2 is pointing to 10 (list1)

cseinterviewprep
Автор

This method will not pass a few test cases like:
when you have two similar nodes before the intersection point listA = [4, 1, 8, 4, 5], listB = [5, 6, 1, 8, 4, 5], Above code will get you 1 while it should be 8

vaibhavkumar
Автор

Thank you so much sir for teaching in such a simple way 🙏

abhishekvishwakarma
Автор

Hi bro your explanations are very clear and helpful. Thanks

biswadeepchakraborty
Автор

thanks for the video. Correct me if Im wrong, this approach will not work in this situation :
listA = [4, 1, 8, 4, 5]
listB = [5, 0, 1, 8, 4, 5]
intersectVal = 8
because before it gets to the intersection point, node values are already the same (i.e 1) and it will jump out of the loop.

rrsonia
Автор

Python code without having to compare lengths. You basically jump to the other list when you come to the end of the list. This way, if there is a connection it is guaranteed to meet at the link.
````
class Solution:
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
if not headA or not headB:
return None

savedA, savedB = headA, headB

while headA != headB:
headA = savedB if not headA else headA.next
headB= savedA if not headB else headB.next

return headA
```

maged_helmy
Автор

good logic actually i tried of doing with comparing pointer of 1st linked list with that of second resulting in increase of time complexity and this approach was cool

Techjai
Автор

u cud use a hashmap to store the address and compare it when traversing both the linked lists

HarshaVardhan-jfsd
Автор

What if two linked list have same node but it's not intersection? It's possible to have same node and before intersection itself..

northstaar
Автор

what is it was 'd' in 2nd linked list in place of n??

karthikrashinkar
Автор

Could you make tutorials on hard sections of HackerRank Problem Solving including both Data Structure and algorithms. Your tutorials are great and thanks for your effort

biswadeepchakraborty
Автор

sir could you please make a video on XOR linked list, SKIP list, UNROLLED list..

rflxplays
Автор

please put the link to the video on how to calculate the length of the list in the description thanks for the awesome vide and please write out some code to help us out thanks
you are so awesome and I love you

dantewhite
Автор

this is merging point of two link list.., intersection is different

dharnachandrakar
Автор

Please take github in description or code link

CodeWithGotu
Автор

what a nice algorithm, thank you sir!

debaratighatak
Автор

Lets assume that the length of list is bigger. What is the easy way to find in that case?

shanpalaniram
Автор

How do we find the intersection of two lists of equal lengths in C

youloose
Автор

what if "e" present ar "a" the location of first linkedlist and "m" the location of second linkedlist

murugesh