 What's up guys, mine is Michael and welcome to my YouTube channel today. We're gonna go over find merge point of two lists Basically, you're given this really weird to really weird link lists. I Don't know that why they have like two next pointers. I don't know what's what it is, but essentially You have like Two link lists that connect to a single node. We want to find that node, right? So like we have in this case, we have a link list one that has a b and c and then the link list to has p and q then there's also a It's also you could also go to x and b you could go to x also and this is where it converges to at x, right? It just goes together It meets here in this middle here of x and then it continues goes y z and all so What you're trying to do is you're trying to find a create a function that's given these two link lists It just returns like the node that it that it's The know that it's at right that it converged to So I'm gonna just explain that what I did and then I might explain a better solution So what I did was I created an unordered set That's just gonna hash the pointer of each node that we're looking at So I start up the first node first list right this first list of A and then I'm just gonna every time I go to the next node I'm just going to add it into my link list and I link list into my set So here I created a current node called just current node And I pointed to the head one the first head of the first link list and then while it's not equal to null pointer I'm just gonna insert this This is the pointer into my Unordered set, which is gonna be like a hash set and I just go to the next node Then what I do is after I added all the nodes that are that I've seen already Right into my set that I've went through through the first list. I go through the second list Right. I'm gonna set my head pointer to point to my current pointer point to head to and I'm gonna do the same thing Except this time I'm gonna see. Hey, did I already see this node? so I Searched through my set and I see Set my set scene and I just check hey if seen not fine is not equal to the end That means I've already seen this node and if I've already seen this node Then I return the data at that node and the reason why this works is because let's say we're at this example of test case 0 It's gonna go through the first link list Right, and then it's gonna add all those to the set. So it's gonna add one. It's gonna add two It's gonna three. It's gonna add null and once it reaches the end right it stops and it's gonna go through the second list Right this second was starting at this point of one and it's gonna check. Hey, have I seen this already? So have I seen one well technically I haven't right because these two ones don't have the same pointer Right, so then I'm gonna go to this to hey have I seen this to already have we seen this link list to of This head pointer to I have Therefore, this is the point where the two linked lists meet and then I just returned the two So that's what I do here. So if like if the scene dot fine current is not equal to the end It was already found this. I'm just gonna return the data at the current node And then I just go to the next list Then when I've done going through the second list, right? I'm going to the next one when I've done and I reached the end of null pointer That and let's say I didn't return any Data, right? I didn't find any No that I've seen already And that means there is no converging point, right? That means that both of these link lists don't meet at one point So if that's the case, I return negative one because that means that the link lists haven't We haven't seen the link list. We already looked at we already went to where we haven't seen a pointer There we haven't went to so that's why we return negative one All right, I'm gonna show you guys another solution also which is in case if This one's in Java, but it's essentially the same thing except we're gonna have two pointers one's gonna point to the head a and one's gonna point to head B and we're gonna keep traversing until Reached a point where they're the same. So this is this this problem statement is actually assuming that Two link lists are gonna merge at one point, right? So if we're assuming that it's gonna merge at one point what this what this person did was he's just gonna keep Going to the next node until we reach Until these two pointers are the same, right? so we have one pointer starting at the head node of the first list and second Pointer pointing to head B of the second list and they're just gonna keep going next keep going next until Those two pointers are the same and then they return it. So when they're going next, there's checking hey Yeah, the first one what they're gonna do is they're gonna go keep going next for the first pointer and then the second pointer of Pointer B is gonna keep going next When they reach the end of the list Right, so if the first pointer is next is equal to null that means they reach the end of list What they're gonna do is they're gonna they're now gonna switch to the second list instead and Look through that direction because they switch to the second list There's a big chance that both of these two will meet in the middle right because After I'm done looking through the first list I have to look through the second list to see if I ever meet met in the middle that these two linked lists traverse together Right. So yeah, and then if the second pointer that they're pointing at is equal to null So this meaning that if the second pointer I've already looked through all my second list, right? I Already looked through all the nodes of my second list and it's equal to null then they're gonna go to the first list and Like through those pointers also So if they go to the first list The they're gonna set the second pointer to start pointing at the first list and they're gonna keep going down and the sooner Later, it's gonna meet with the other pointer the first pointer and in the middle Right, so yeah when this is done They just returned the current bees data because that means they're at the same position So this works because it's assuming that there is a converging point for two of the link lists So what it's gonna do is gonna the first point is we're starting to start a second point to start a P It's gonna keep going next next next and then once a link list is finished. It's at C and it's finished It's gonna start at P and it's gonna start going to the second list and once the the second list is finished, right? Once that pointer is finished that point is gonna go to the first list and they're gonna keep traversing down That's your later. It's gonna meet in the middle. Okay, so yeah It's been pretty much just the problem Personally, I think my solution is a little easier to understand because like we're just looking through all the notes that we've seen already And if we already seen it, we just return it But yeah, if you want to go to the two pointer out. That's also another way you could do it It's just a little difficult to Traverse through it, but yeah rate comp subscribe. I'll check you guys later. Peace