 What's up guys, my name is Michael and welcome to my YouTube channel today where you're going to go over how to merge two sorted link lists. So let's say we have this link list of 137 and it's null and then we have a link list B which is one, two and a null. So we want to merge these two so that it becomes sorted, right? So we know the first two link lists are sorted and we want to return the final link list where they're all sorted in sort of order. So imagine you just like have all these numbers to be one, one, two, three and then seven. All these values are just going to be sorted. So we have basically we have the same, same numbers in both of the lists, but now they're all sorted. Right? Okay. So that's basically what we want to return. So we have same numbers one, three, seven, one, two, but now they're all in sorted order and it's in one giant list. So yeah. All right. I'm going to go over how to do this problem because I feel like there's not much other to explain. Okay. So first of all, let's say we got, I'm going to do it recursively by the way. So let's say we have one, three and seven and this goes to null. And the second one we have one, two and a null, oops, and this goes to null. Okay. So let's say we had like, okay, let's say we had, let's, so since we're doing this recursively, we have to think about the base cases. So let's say we're at the end of the list. So where both lists are at the end, right, for both of these, like we're trying to merge them all together, right? So in our final list, if both of these are null, right, if both of these, these two nodes from our first list and the second list, if they're both null, then we should add a null. Right. Do you guys understand what I mean? Like if, if we compare this to this, it's going to be null. Our final list has to have a null. The reason why is because like, well, they're both null. So our final list has to have a null. Right. So if the, if any of the nodes that we're going looking through our null, then our final list has to point to a null. Okay. It has to return null. Likewise, if now let's say that we're comparing null with like two, right? So if we're going to insert all these values to the final list of like one, one, two, three, seven null, if we're comparing a null with like just about regular value two, we need to return whichever that's not null. Right. So if I'm at like this point, and this part is null, right? If I want to add my value to the end link list, then this whatever I'm returning, it has to not be null. Right. Like, let's say we had a one, one, two, three, seven, right? So we have this list and this list. If I'm going to insert, let's say we already inserted like all the other values like one, one, then this is not a good example. But anyway, if we're at this point where we have two and then null at this location, then we have to add whichever value that's not null, right? We have to add two here, and then go to the next one to the next node that we're adding. You guys understand what I'm saying, right? Because like if if you reach to a null, that means that you're at the end of the list for one of the lists, right? So you have to continue on the other list. Do you know what I mean? So like if, so let's say I reach the end of the list of this list of null. My other, other node is still looking at the other list because it's doing not the same size, right? Then I have to add whichever value that's not null, right? I have to add like seven here or something, right? So that's what depends on the link list. So those are the two, those are the three base cases. So the three base cases are, if they're both null, so if the first node that I'm looking at is null, and the second one is null, then I have to return null. And that's the case when both of these are like at the end, right? They're at the end, then I have to add a null in my, I have to add an end to my last link list that I'm returning, right? I have to add a null because they're both null. If one of them is null, so if one of them is null, if the second list head is null, but the first one's not, and I have to return the first one, because that's the, that's just continuing on the rest of the first list, right? And if the first one is null and the second one's not null, then I have to return the second one. And the reason why is because this is just adding whichever list that, whichever node is not null, right? If we're, if we reach one part of that's null, that means we're finished with one of the lists. And the other list, we still need to continue adding those values in. So that's why for those values, we have to, we just have to return that other node, right? Whichever node that's not null. So heads two is not null, then we return head two, right? All right, this part is where it gets a little tricky to understand. But I guess you have to bear with me. So we have head one. And so we already went through the base cases, right? So let's actually go over, go through an example test case of the current test case, and then we'll see how this actually affects anything. So if we look at the first test case of 1, 1, 1, 1, 3, 7, null, then 1, 2, null, we have, let's go through the first statement. So if the head of the linked list data is less than or equal to head two of the data, I'm going to recursively call on the next list. And set my head ones next to point to that. So what does this mean? This means that, so let's say, let's say this is there. This is our head, right? This is our head one, head, this is head one, and this is head two. Let's call it H2, OK? H2 and H1, OK? So let's say H1 is less than or equal to H2, right? So in this case, it is, right? 1, 1. So what am I going to do? Well, what I'm going to do is I'm going to set this pointer of ones next, right, this pointer of ones next, is going to point to recursively calling the rest of the list passing in these values, right? So this head one, this head one's next. This one is going to point to the rest of the list passing in the next of the value, right? Merging in the rest of the list of that value. So then it would be one point to whatever that is merged with 3, 7, 0 and 1, 2, 0, right? Whichever is returned to this part, right? Because what it's doing is that it's going to merge the rest of the list of head one with head two's list so that you could return your final merged list together. So that's what this is doing, right? If our current data is less than or equal to seconds data, so if this one is equal to one or it's less than it, then we're just going to recursively call on the rest of the list and pass it in. All right, let's say it was greater than it, though. So let's say the current value is greater than the current data. And that scenario would happen if when we traverse through the second point, so let's say we already merged one, one together. So one point to one. So these are already merged, right? So let's actually give it this. They're already merged together. Now we're at 3 and 2. So in this situation, 3's data is greater than 2's data, right? 3's data is greater than 2's data. So what is it going to do, right? OK, so what's going to do is it's going to set a pointer temporary to point to the second node. So this is the second node, right? This is the second node, head two. So it's going to set a temporary node to point to this, right? This is a secondary node point to this. Then it's going to set head two's next point to go to the next node. So this two is going to go to the next one. So this is going to be h2 is going to go to the next node, right? So now this is technically gone now, or this link is gone technically. So h2 is now goes to points to the next node, which is null, and 10 points to 2, OK? Then what's going to do is it's going to set temp2's attempts next. This two is going to point to head one. So this two is going to point to now head one, which is 3. So 2 is next, going to point to 3. And then it's going to set head one to equal to temp. So our new head that we're going to get now is going to point to this. This is going to be our new head, right? Do you guys see why? Because it's going to now have 2, then go to 3, then go to 7, right? Before it wasn't merged. Now it's merged. Now what's going to set is it's going to recursively call on head ones next on the rest of the list. So it's going to be head ones is h2 is 2. And it's going to recursively call on the next list of this. So yeah. So the 2 is going to next is going to point to recursively calling on the rest of the list of this. So in the end, we return head one, which is going to be, after recursively calls, multiple recursive calls, it's going to be this one. It's going to be head one. And then we're going to return this new head. So yeah. I hope I explained it properly. It's a little bit hard to wrap. But basically what we're doing is we're just changing the links of each of the nodes so that we're actually merging the rest of them. So when you recursively call on the rest of the list, we're merging the rest of the nodes with what we previously stated. So yeah. So yeah, to create a temporary point to head two, we're going to go to the next node. And then we're going to just point 2's next is going to, temporary next is going to point to h1, set h1 to temp. And that h1 equals the next node. Point to the next node. So yeah. And then at the end, we just return h head one, h1, which is this one here. So yeah, I hope you guys enjoyed this video. Rate, com, subscribe. Let me know if I explained this properly. If not, I'll try to explain it again. But yeah, that's pretty much just this problem. Peace.