L12. Find the intersection point of Y LinkedList

แชร์
ฝัง
  • เผยแพร่เมื่อ 5 ก.พ. 2025
  • Problem Link: tinyurl.com/yc...
    Entire LL Sheet: takeuforward.o...
    Check our A2Z DSA Course: takeuforward.o...
    Please do give us a like, and subscribe to us if you are new to our channel.
    Do follow us on our socials: linktr.ee/take...

ความคิดเห็น • 172

  • @hardikpatel352
    @hardikpatel352 8 หลายเดือนก่อน +48

    intuation behind the optimal approach🔥🔥, legend ....

  • @tanish_gupta
    @tanish_gupta ปีที่แล้ว +16

    Best video for this problem on youtube till date. Covered everything from brute-force to the best-optimised way to solve this problem. Kudos Striver!! Enjoying this path and the playlist.

  • @BholaSingh-m8z
    @BholaSingh-m8z 7 หลายเดือนก่อน +10

    Sir you, covers brute-force, optimized approaches, and even a killer third option! Well done, keep up the good work!

  • @vibhavsharma2724
    @vibhavsharma2724 7 หลายเดือนก่อน +20

    A big salute to striver and his third approach 🫡

  • @amanshrivastav0405
    @amanshrivastav0405 8 หลายเดือนก่อน +17

    You need to be a superhuman to discover the third approach on your own! Thankssssss alloooooottt striver 😇

  • @shaikhsoheel8673
    @shaikhsoheel8673 ปีที่แล้ว +6

    Quality content brother I was waiting for Linkedlist God bless you brother

  • @preethilatadanguria6946
    @preethilatadanguria6946 9 หลายเดือนก่อน +1

    saw many videos to understand the 3rd approach but then landed on this video which has ended by search.thank u stiver for brilliant explanation.

  • @codingsoham
    @codingsoham 9 หลายเดือนก่อน +28

    The last algorithm man. After learning about it, it feels like why I didn't I think about that possibility. Thank you Striver for such a great explanation.👌👌

  • @blurbmusic1507
    @blurbmusic1507 5 หลายเดือนก่อน

    optimal approach just GENIUS!! hats off captain!!

  • @ashwaniagrawal27
    @ashwaniagrawal27 ปีที่แล้ว +2

    Timestamps
    Hashing Approach - 1:20
    Length Difference Approach : 8:10
    Optimal Approach- 16:25

  • @ujjawalchourasiya3988
    @ujjawalchourasiya3988 11 หลายเดือนก่อน

    Aayla Jaado in 3rd approach Maja arha hai ab problems krne m . Thanks striver bhaiya for delivering such approaches things gets better and understandable ❤

  • @chetanyadav5493
    @chetanyadav5493 10 วันที่ผ่านมา

    salute for optimal approach 🔥

  • @pratyushbhatt1712
    @pratyushbhatt1712 3 หลายเดือนก่อน

    Beautiful Optimal approach. Thanks Striver!

  • @krishkumar4603
    @krishkumar4603 19 วันที่ผ่านมา

    the optimal approach is 🔥

  • @RajeevCanDev
    @RajeevCanDev ปีที่แล้ว +22

    teachers like him comes in a decade or more, bhaiya please try to upload stacks and queues and strings as well

  • @shashankyadav8977
    @shashankyadav8977 5 หลายเดือนก่อน +4

    The hashing one can be done in one iteration as well. We can insert in map acc to both LL at once. And check at that moment only if the freq is >1.
    Code - C++
    int intersectPoint(Node* head1, Node* head2) {

    Node*p1=head1;
    Node*p2=head2;
    unordered_map hashmap;

    while(p1||p2)
    {
    if(p1) hashmap[p1]++;
    if(p2) hashmap[p2]++;
    if(hashmap[p1]>1) return p1->data;
    if(hashmap[p2]>1) return p2->data;

    p2=p2->next;
    p1=p1->next;
    }

    return -1;
    }

    • @LinhHoang-ml1qo
      @LinhHoang-ml1qo 4 หลายเดือนก่อน

      but it takes more space complexity while the last solution of Striver only takes O(1)

    • @Siliboy77
      @Siliboy77 หลายเดือนก่อน

      you are returning the data integer not the node

  • @TON-108
    @TON-108 ปีที่แล้ว +2

    After following playlist till here, now i'm able to do these question myself 🌚
    Thank You Striver Bhaiya!!!

    • @navneetuppal9753
      @navneetuppal9753 ปีที่แล้ว +3

      Bro how are you able to think the approach and till which approach could you think of like till brute better or optimal and how?

    • @LukmaansStack
      @LukmaansStack 10 หลายเดือนก่อน

      i was able to solve bruteforce but the 2nd and 3rd approches were out of my mind☹☹@@navneetuppal9753

  • @AkashRaj-vj6br
    @AkashRaj-vj6br 10 หลายเดือนก่อน

    Thank you Striver for explaining so well...

  • @technosujeet
    @technosujeet ปีที่แล้ว +37

    third approach literally blown my mind

    • @bruvhellnah
      @bruvhellnah 8 หลายเดือนก่อน +4

      "literally"

  • @stith_pragya
    @stith_pragya 9 หลายเดือนก่อน +1

    Understood.....Thank You So Much for this wonderful video..........🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻

  • @hareshnayak7302
    @hareshnayak7302 9 หลายเดือนก่อน

    Wow Just wow what a mind blowing approch striver , Cool.thanks for this amazing video.

  • @AmanVerma-iu8jp
    @AmanVerma-iu8jp 8 หลายเดือนก่อน

    the way you explain the concepts is lit🔥

  • @harshverma9675
    @harshverma9675 21 วันที่ผ่านมา

    the main challenge I found while solving DSA questions is that I am not able to find out approach to solve. In majority of cases when you told me the approach I can then easily solve that question without problem. If you are reading this please tell me How can I train myself so that I can easily understood the approach ?? I JUST NEED THE APPROACH AND THE REST IS EASY.
    Thank you for the approaches.

  • @tanyaahuja7424
    @tanyaahuja7424 ปีที่แล้ว +4

    Thank you striver !!

  • @RaunitJaiswal-s9v
    @RaunitJaiswal-s9v 25 วันที่ผ่านมา

    The thing which has hitted my head is that start both the linked list at a same time and move them equally if
    First ll comes to end the return second ll
    If first ll =second ll return any linked list
    If second comes to end return first

  • @ankush8243
    @ankush8243 ปีที่แล้ว +1

    Thank you bro!💙
    keep teaching like this💯🤗

  • @rumiNITPatna
    @rumiNITPatna หลายเดือนก่อน

    amazing solution!

  • @darshilmodi6851
    @darshilmodi6851 7 หลายเดือนก่อน

    third approach is the real goat

  • @ShaikBanu-j2o
    @ShaikBanu-j2o 6 หลายเดือนก่อน

    Third approach is mind blowing

  • @AlaskaAlaska-xq4gk
    @AlaskaAlaska-xq4gk 6 หลายเดือนก่อน

    zeherrrrrrr teaching

  • @oyeshxrme
    @oyeshxrme 6 หลายเดือนก่อน

    thanks bhaiya for providing such an amazing content

  • @piyushrawat4668
    @piyushrawat4668 2 หลายเดือนก่อน

    damnn🔥🔥the third approach!!!

  • @hat_awesome21
    @hat_awesome21 ปีที่แล้ว +54

    striver can we expect stack and queue from u next ?

    • @janaSdj
      @janaSdj ปีที่แล้ว

      Yes please

    • @tonylee1868
      @tonylee1868 ปีที่แล้ว +10

      String much needed
      Stack and queue already there

    • @thorxn1382
      @thorxn1382 6 หลายเดือนก่อน

      😢

  • @ranjeetkumaryadav1837
    @ranjeetkumaryadav1837 6 หลายเดือนก่อน

    Thanks sir
    Last one is 🔥🔥

  • @ritikshandilya7075
    @ritikshandilya7075 9 หลายเดือนก่อน

    Thankyou so much for great explanation @Striver

  • @kushalswarup2662
    @kushalswarup2662 7 หลายเดือนก่อน

    mind blowing method. cant believe woahh

  • @anastasiiamaslova4220
    @anastasiiamaslova4220 ปีที่แล้ว +1

    great explanation!

  • @yashwanthyerra2820
    @yashwanthyerra2820 2 หลายเดือนก่อน

    third approach is like forming loop using junction and making them collide op approach🔥

  • @knowthrvdo
    @knowthrvdo 11 หลายเดือนก่อน

    NICE LECTURE AND PLZ UPLOAD REMAINING LECTURES OF A TO Z SDA SHEET PLZ THEY ARE VERY HELPFULL FOR US

  • @kushalkollu8628
    @kushalkollu8628 8 หลายเดือนก่อน

    damn man! what an idea for the optimal approach

  • @MohammadEhshan-ve6uz
    @MohammadEhshan-ve6uz 4 หลายเดือนก่อน

    Done it in 5 lines:
    Node* first=head1;Node* second=head2;
    while(first!=second){
    first=(first!=NULL) ? first->next : head2;
    second=(second!=NULL) ? second->next : head1;
    }
    return first;

  • @utkarshpundeer07
    @utkarshpundeer07 6 หลายเดือนก่อน +1

    3rd approach was a 🤯🤯

  • @YourCodeVerse
    @YourCodeVerse ปีที่แล้ว

    Understood✅🔥🔥

  • @prathamesh009m
    @prathamesh009m ปีที่แล้ว

    Nice teaching

  • @prathamesh009m
    @prathamesh009m ปีที่แล้ว

    So help full video

  • @rohandas6298
    @rohandas6298 7 หลายเดือนก่อน +5

    Why the time complexity of the last approach is not O(2 * (n1 + n2)) ??

    • @BhavanaReddy15
      @BhavanaReddy15 7 หลายเดือนก่อน

      Same doubt

    • @Fe-ironman
      @Fe-ironman 6 หลายเดือนก่อน +3

      @@BhavanaReddy15 because you're traversing both linked list simultaneously not separately.
      for example while traversing first linked list you traverse m step and reach null then traverse at maximum n step at worst case when there's no match and simultaneously you're traversing through the other linked list first n step and then m step at worst case when no match so the complexity is O(m+n)

  • @tejaspatel6965
    @tejaspatel6965 4 หลายเดือนก่อน +1

    damn the third approach is beautiful

  • @knight-z1x
    @knight-z1x 7 หลายเดือนก่อน

    beautiful

  • @PCCOERCoder
    @PCCOERCoder 2 หลายเดือนก่อน +1

    Lecture successfully completed on 28/11/2024 🔥🔥

  • @harigs72
    @harigs72 4 หลายเดือนก่อน

    Understood.🎉❤

  • @pratyushtripathi1728
    @pratyushtripathi1728 ปีที่แล้ว +1

    Understood 😊

  • @momentcoder
    @momentcoder หลายเดือนก่อน

    Understood 👏

  • @hashtagcc
    @hashtagcc 10 หลายเดือนก่อน +1

    The first time who have invented this algo must be from another dimension😙

  • @ramkrishnapatil2105
    @ramkrishnapatil2105 4 หลายเดือนก่อน

    dimag hil gya bhai 🤐🤐

  • @saswatrath4646
    @saswatrath4646 9 หลายเดือนก่อน

    3rd approach was awesome haha

  • @mainakdasgupta7130
    @mainakdasgupta7130 23 วันที่ผ่านมา

    thank you man

  • @baalsupremacy5760
    @baalsupremacy5760 18 วันที่ผ่านมา

    I have a doubt, when both the linked lists dont have a intersection point, the while loop should run forever right?, because we reassign if pointers reaches null, so the pointers wont be reaching null so they wont become equal right ? then how the loop stops?

  • @vaibhavbhatt734
    @vaibhavbhatt734 ปีที่แล้ว +1

    i have taken a course of dsa and in that course one teacher tell the 2 approach till this date I think this approach is optimized but today (wake up to reality ho gaya)

  • @khalasianiket816
    @khalasianiket816 6 หลายเดือนก่อน

    understood ❤

  • @selene8721
    @selene8721 6 หลายเดือนก่อน

    Thank you so much!!

  • @DeadPoolx1712
    @DeadPoolx1712 4 หลายเดือนก่อน

    UNDERSTOOD;

  • @SHIVAMSINGHPARIHAR-w1i
    @SHIVAMSINGHPARIHAR-w1i 5 หลายเดือนก่อน

    Was asked to me in Oracle Intern Technical Round 1 Interview

  • @ArpanChakraborty-do6yz
    @ArpanChakraborty-do6yz 10 หลายเดือนก่อน +1

    Understand 31:53

  • @NazeerBashaShaik
    @NazeerBashaShaik 9 หลายเดือนก่อน

    Understood, thank you.

  • @ManishLakkavatri
    @ManishLakkavatri ปีที่แล้ว +1

    Understood!

  • @DeepakPatel-d5v
    @DeepakPatel-d5v 9 หลายเดือนก่อน

    Thanks a lot bhaiya

  • @rishipatwa6823
    @rishipatwa6823 4 หลายเดือนก่อน

    can we do like reverse both ll and then check them until they are equal ..once unequal return the prev node

  • @Learnprogramming-q7f
    @Learnprogramming-q7f 11 หลายเดือนก่อน

    Thank you Bhaiya

  • @HeetMungra-g9l
    @HeetMungra-g9l 3 หลายเดือนก่อน

    hey!!
    what if we start traversing from end of the both list and return the last node where they are equal??

  • @manga_mania.
    @manga_mania. ปีที่แล้ว

    thank you striver

  • @suruabhisekh
    @suruabhisekh หลายเดือนก่อน

    Node* findIntersection(Node *firstHead, Node *secondHead)
    {
    //Write your code here
    Node* t1=firstHead;
    Node* t2=secondHead;
    while(t1!=NULL && t2!=NULL)//when both are null
    {
    t1=t1->next;
    t2= t2->next;
    if(t1== t2) return t1;
    if(t1 == NULL) t1=secondHead;
    if(t2==NULL) t2=firstHead;
    }
    return nullptr;

    }this is best I think

  • @om3478
    @om3478 หลายเดือนก่อน

    understood!!!

  • @NARUTOUZUMAKI-bk4nx
    @NARUTOUZUMAKI-bk4nx ปีที่แล้ว

    Understooood

  • @shelkeonka
    @shelkeonka 3 หลายเดือนก่อน

    In 2nd approch when N1 > N2 then in collision funtion we have to bring the t1 at alin postion which matches the t2 but the code is just work for when N1 < N2 ??

  • @prajapati-suraj
    @prajapati-suraj 5 หลายเดือนก่อน +3

    if anyone does figure out the third approach on his own, he is a genius.........

  • @SurajSharma-z9p
    @SurajSharma-z9p ปีที่แล้ว +3

    Please correct me if I'm wrong. If the two linked lists are never intersecting and are of different lengths, this will be a infinite loop because t1 will never be equal to t2 and we'll keep on interchanging them with heads in case of null

    • @shivangairan6904
      @shivangairan6904 ปีที่แล้ว

      Bro even if they are non-touching, after the first exchange for both they will come in parallel so the loop will terminate when they reach null together

    • @SurajSharma-z9p
      @SurajSharma-z9p ปีที่แล้ว

      @@shivangairan6904 Understood. Thanks

  • @hajeeramohamad7641
    @hajeeramohamad7641 4 หลายเดือนก่อน

    in the second approach - what if the length of both the linked lists is same? how will the collision function work then?

    • @prajeetdubey8213
      @prajeetdubey8213 หลายเดือนก่อน

      The difference will be 0. So basically it starts comparing from the head only and then simultaneously incrementing until common node is reached

  • @kale-lb5pr
    @kale-lb5pr ปีที่แล้ว

    how does storing nodes directly make difference if we encounter same node value form second linked list why hashmap was showing no???/

  • @fsyk1306
    @fsyk1306 7 หลายเดือนก่อน

    Hey Striver, for the last algorithm, wouldn't it work too if when T1 or T2 reach NULL , they can be put back to Head1 or Head2 respectively? T1 goes back to Head1 and T2 goes back to Head2, it's working the same.

    • @aryasharma69
      @aryasharma69 5 หลายเดือนก่อน

      Try with different example like N1=5 & N2= 7, then you will get to know why it fails

  • @SuvamoySamanta-n2x
    @SuvamoySamanta-n2x 4 หลายเดือนก่อน +1

    why use this line mpp.find( temp) != mpp.end(); please explain this line

  • @factswithjenny9
    @factswithjenny9 ปีที่แล้ว

    thanks

  • @manikanta2000
    @manikanta2000 3 หลายเดือนก่อน

    My simple java code
    public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
    ListNode temp1=headA;
    ListNode temp2=headB;
    while(temp1!=temp2){
    if(temp1==null){
    temp1=headB;
    temp2=temp2.next;
    }
    else if(temp2==null){
    temp2=headA;
    temp1=temp1.next;
    }
    else{
    temp1=temp1.next;
    temp2=temp2.next;
    }
    }
    return temp1;
    }
    }

  • @VineetChelani1304
    @VineetChelani1304 ปีที่แล้ว +1

    can we even compare each node of one LL to Other LL and get the first intersection ??

    • @dhrumilparmar1505
      @dhrumilparmar1505 ปีที่แล้ว

      that's brute force approch..still works but time complexicity will be of O(n^2).here we will get in O(max(len(t1),len(t2)) + distance of intersection from smallest LL)....somewhat linear complexity

  • @naturesrevolution
    @naturesrevolution 8 หลายเดือนก่อน +1

    anyone pls answer in second approach what happens if t1 and t2 contains same element before linking?

    • @qwerty20917
      @qwerty20917 8 หลายเดือนก่อน

      No problem in that case too bro.. cause we are comparing the pointers themselves (whether they point at same position/node or not),not their values..
      Hope this helps..

    • @naturesrevolution
      @naturesrevolution 8 หลายเดือนก่อน +1

      @@qwerty20917 thank you bro

  • @kale-lb5pr
    @kale-lb5pr ปีที่แล้ว +1

    pls can anyone explain me what is the difference in storing a node in hashmap and a value ? if the same value is present in second LL also why doesnot it indicate yes? pls give me hintts...

    • @lot2say590
      @lot2say590 ปีที่แล้ว +5

      Because Node is a class which consists of the address of the data too. Hence while comparing the same value, we are not checking if it's in the same address, so we can't conclude on that. So to check if it's in the same address, we compare the whole Node and not just the value.

    • @djpro6413
      @djpro6413 ปีที่แล้ว +1

      @@lot2say590 Thanks bro same question arise to me

  • @RAHULSHARMA-on6tm
    @RAHULSHARMA-on6tm 8 หลายเดือนก่อน

    in this code if you remove the if t1==t2 return t1, it should work fine right? Because the while loop will break in case both of them become equal, but the problem is, removing this condition will cause an error to appear. Can someone please answer this issue. Thanks

  • @DEVANSHVYAS-z1b
    @DEVANSHVYAS-z1b 7 หลายเดือนก่อน

    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
    ListNode *p1 = headA, *p2 = headB;
    while(p1 != p2){
    p1 = (p1 ? p1->next : headB);
    p2 = (p2 ? p2->next : headA);
    }
    return p1;
    }

  • @akashumre22
    @akashumre22 ปีที่แล้ว

    Bro thanks

  • @nehapimpalkar396
    @nehapimpalkar396 ปีที่แล้ว

    understood!

  • @chiragbansod8252
    @chiragbansod8252 10 หลายเดือนก่อน

    understood

  • @ekjotsingh9609
    @ekjotsingh9609 10 หลายเดือนก่อน

    Why wasn't a set used in the hashing approach? Wouldn't that be better?

    • @aryasharma69
      @aryasharma69 5 หลายเดือนก่อน

      That will still take O(n) space

  • @kale-lb5pr
    @kale-lb5pr ปีที่แล้ว

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

    node* temp = head1;
    map 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;
    }
    temp = temp->next;
    }
    return NULL;
    }
    int main()
    {
    vector arr={3,1,4,6,2};
    vector b={1,2,4,5,4,6,2};
    node *head1=convertarrtoLL(arr);
    node* head2=convertarrtoLL(b);
    node* head=intersectionY(head1,head2);
    cout

  • @harshuke5831
    @harshuke5831 ปีที่แล้ว

    Understood

  • @SiddhesDas
    @SiddhesDas 3 หลายเดือนก่อน

    small correction
    if (n1 < n2) {
    return collisionPoint(headB, headA, n2 - n1);
    } else {
    return collisionPoint(headA, headB, n1 - n2);
    }
    baaki first class

  • @harshitjaiswal9439
    @harshitjaiswal9439 ปีที่แล้ว

    understood.

  • @roshnisingh7364
    @roshnisingh7364 ปีที่แล้ว

    please share the code for last approach

  • @Chlo_Chle_Hum
    @Chlo_Chle_Hum 6 หลายเดือนก่อน

    is circular linked list important?...if yes,from where to do..someone pls suggest

  • @devfusion-8718
    @devfusion-8718 7 หลายเดือนก่อน

    mapm;
    while(headA!=nullptr)
    {
    m[headA]++;
    headA=headA->next;
    }
    while(headB!=nullptr)
    {
    if(m.find(headB)!=m.end())
    {
    return headB;
    }
    headB=headB->next;
    }
    return nullptr;

  • @stellar3496
    @stellar3496 7 หลายเดือนก่อน

    waatha idhan da coding 🤯🤯

  • @NonameNoname-f2t
    @NonameNoname-f2t 11 หลายเดือนก่อน

    SUUUUUUUUPERRRRRRRRRBBBBBBBBBBB