 What's up guys, my name is Michael and welcome to my YouTube channel. Today I'm going to explain to you how to compare two link lists to check if they are equal recursively. So here let's say I have this. Like close one, two, three, and this goes and all right. And then let's say I have this link list. One, two, and then four. Okay. And this goes and all. Well, first of all, what should I do? You should think about this. We're going to do this recursively because recursion is always easy for the link list. Okay, so let's say I'm going to do this recursively. I have the first list of one and the second list of one. All right, I'm going to keep calling next over and over and over again. And let's say I reached the end of the list. Okay, I reached the end of the list. I keep going next for both of these over and over again until I reach the end of the list. Well, if my first two nodes here are null, these are equal, right? Think about it. If they're null, then they're just going to be equal. So that's the base case, right? If they're null, then they're equal, right? If they're both or both of them know, then they're equal, right? Now let's say this, this, this wasn't null, right? So the second case is what if this was like a five, right? If this is a five, well, if this is a five and this is a null, then they're definitely not equal, right? This first list and the second one is definitely not equal. No matter what you do, you don't have to check the rest of the list because you know that these two are not equal at all, right? Because null, this one's own, this one's five. Okay, so if they're both known, we know they're equal. If one of this list is not null, then it's definitely not equal. Now, what if the other list is not null? Let's say this was null and this list was actually point to five, right? Then they're definitely, they're also not equal, right? Because this five is not equal to this null. Like this node, if we're just comparing this last node to five, compared this node, this node of null, it's definitely not equal, right? So if they're both null, we're going to return true, right? When we reach the end of the list, they're both know that we're going to return true, right? If we're both know that we're going to return true. If I, else if, otherwise, if one of them is null, or the other one is null, then we're returning false, right? If you think about it, like, if they're both know, we're going to return true. Now, if one of them is not null, otherwise, let's say they're not both know, one of them is not null, right? It's one of the points of five. Then we know it's going to be false, right? If this is not null, there's five. But also, if the other one's not know, so let's say if this is not know, this is five, then we're going to return false. So those are there are two base cases. If they're both know, we are going to return true. If either of them is not know, right? Either of them is equal to know, right? If either of them is equal to know, then we're going to return false, right? Now, what are we going to do next? Otherwise, we're going to then let's say they're both not know, right? Then let's say we get down to the next node. So let's say we're the stack just got pushed back and we're going to hear, right? Well, now we just have to check is, is our data at the null at the nodes, the nodes data for both of them are the equal. So if they are both are equal, then we're going to return true. Otherwise, if they're not equal, well, we're turning false, right? If they're both equal or true, otherwise, we're not returning false. Then we're just going to like recursively call this function and just return this one and this condition with the rest of the list. So we're just going to keep passing the null to the rest of the list and keep comparing doing this. So I'll show you guys what the code looks like for that. So here, if the first head is null, and the second head is null, then we're going to return true. Now, else, if, if either of them is null, like had one's null, or had two is null, then we're going to return false. And I explained to you guys why that's the case. If one of the other head is null, right, or the other head is null, right, then we're turning false. Because that means one of these is null pointer. And the other one, if they're one of these are null, we're turning false. Otherwise, we're going to come check the data. So we're going to return the heads data, making sure the heads data of the first heads data is equal to the second heads data. Okay. And once we do this, we also have to end this condition with the rest of the list. So we're going to check the rest of the list. Passing in next recursively calling compare lists, passing in the next heads next and head twos next over and over again. And yeah, that's basically basically the gist of this function. I hope you guys enjoy this video to quick tutorial on how to compare two link lists, rate com, subscribe. I'll check you guys later. Peace.