 What's up guys my name is Michael and welcome to my YouTube channel today I'm going to go over how to reverse a link list and this is I'm going to do it recursively where you're returning the node actually every time. So I know this is like pretty difficult to think about but bear with me. So let's say we have this link list of three two and then one right. And this last one goes to null. So I want to you to basically return the reverse of this. So this should become one then two and then three and then three goes to null. So how would you do this? Well, first of all, if we're going to do this recursively, which I'm going to teach you guys how to do it recursively because generally this is how they want you to do it in an interview. This is how you would do it. Okay. First of all, let's think about what would our base case be? Well first of all, we're going to repeatedly go next to the next node recursively. So all the time I'm just going to keep going to the next node over and over and over again, recursively calling this reverse method. And during this time supposed to reverse the link list. Now, if I'm going to do this recursively, what would my base case be? Well simple. If I'm going to keep recursively, we're recursively calling this function, I'm going to reach the endpoint. And what is the endpoint? The endpoint is when my current node, if I'm going to keep recursively going to the next function, so I keep going to the next, the next node, the next node, right? My end function is when my node is in all okay, whenever I've reached the end of the list, I just return null. Okay. Right. When I reach the end of list, I'm going to return null because that means I'm at the end of the list. Well, but what is my other base case? My other base case is that let's say I'm reached from if I keep going next three and two then one is when I reach this one, it's when the ones next is null. Okay. And the reason why is because at this point, we're only at one node left. So we're just going to return only one, like we're going to return the node here, because we have nothing else to do at this point, that you can't do anything here. Right. At this point, the reversal of one is just itself, right? There's nothing to reverse. There's nothing to reverse here. And also the, the reversing, trying to reverse this null, let's say null, it's the last one. Right. The reverse null null is just itself. So that would be the base case, right? Is when you keep reversing it over and over again, you're just in the end, you're going to get like what one or no, okay, that's at the end of the list. Okay. Now, this part is when it gets tricky. What do I want to do? Let's say I'm going to recursively call this function. So what is this function going to return? It's going to return one, and then this function is going to return the rest of the list. That's, if you think about it, let's assume this function works, right? Let's say I just have this function, and it's going to return the reverse of this link list. So what do I do in order to reverse this link list? Let's think about it. If I have a function that returns the link list reversed, what am I going to do once I reach this one, right? It's simple. I'm just going to point this one to the reversal of the rest of the link list. I know this sounds crazy, but think about it. If I'm at this last point of one, right, I'm at this point of one. I want to reverse this link list, right? And this function returns the link list, the rest of the link list that is reversed. Right? What I'm going to do is I'm actually going to point this function, this one, it's going to point to the rest of the list. Yeah, reverse, it's going to point to the rest of the list, reverse, right? Basically, this one, once I reach this one, this one's next is going to point to the rest of the list, the reversal of the rest of the list, right? So what it's going to do is this one here, when I reach the end of this one, I'm going to point this next, the one's next is going to point to a function that's going to return me the reversal of the rest of the list. Right? Do you think about it? Like this one, if I have a one and I want to return one, two and three, right? I have three, two, one. I want to reverse it. I want to get to one, two, three. Then what's the... What do I do? Assume that I have this function that is reversed, that can reverse the rest of the list. Let's assume it works. Well, if it does work, then one's next is going to point to the rest of the list that's reversed. Right? Think about it. Right? This one's next is going to point to the rest of the list. That's reversed. Okay. So now that's easy, like we just got that out of the way. But now what happens to the rest of the list when we get to the end? This is a little tricky. Let's say I'm at this point where one's next is going to point to the reversal of two, three. Right? And then two's next is going to point to the reversal of three and the end point. Right? The end of whatever was. But let's say I get down to two and three. What do I do at this point when I only have two and three? Right? I have two and three. What do I do at this point? Well, one's next is going to point to the reversal of three, three and two, right? Three and two. And it's going to reverse three and two to become two and three. What I do at this point, if my current list, while passing in the rest of this list of three and two, my head is going to point to here. Right? The list's head, the head is going to point to here, right? And if I'm going to reverse this, how should I do this? Like, how would I reverse this? Well, it's simple. Three's next, next is going to point to three. Do you understand what I mean? So this three, three's next, which is going to be two. This two is going to point to three. Now I want to change this two to two's next to point to three, right? Think about it. That's what I want to do. Three's next, next, three's next is two and two's next is one currently. I want to change this so this three's next, next of this two is going to point to three so it's itself. Now what do I do at this point? Now I want to change this three's next to point to null. Do you guys understand what I'm doing? Okay. When we get down to the bottom of it, this is what it's doing. I'm basically rerouting these links or to get myself. And then after this, I'm going to return this one. So then the end result would be one, two, and then three, and then three becomes null. Right? It would be one, two, three, and it becomes null, okay? So yeah, that's basically the gist how you would do this. Once you get down to the bottom of the base case and when you get reached the end of it, your list's next, next has to point to itself of the list and your list's next has to point to null. Once you get down to it, and while you're recursively calling this, you have to recursively call this function while passing in the current node and it's next is going to point to that, the rest of it. All right, I'll show you guys the code now. Okay. So the base case is that I'm going to basically we have list list, this node, this should be called head, a list list, this should be called head. I don't know why they call it list list, it just makes it more confusing. But this is the head of the link list. So the base case is if it's equal to null pointer, or my next pointer is equal to null, then I return my head, okay? The reason why we do this is because think about it. If we get back down to this point, we'll reach the end of the node, right? One's reversal is just itself. And null's reversal is just itself. That's why one's reversal is just we're just going to return the head of it. Now otherwise, let's think about this. I'm going to take my current node. I'm going to assume this function reverse of the rest of the list is going to give me the reversal of the list. Okay. So, so think about this. I have a function, let's assume this function works. Okay. Let's assume this function works. Reverse is going to give me the reversed link list, okay? And that's going to be my current node. That's going, that's definitely going to be my current node. This function current is going to be my new head now, okay? This function current is going to be my new head that I'm going to return, okay? This current function, because it's going to give me the rest of the link list that's reversed. Now, once I get to the bottom of the list, when it's down to two nodes, the list's next, next is going to point to the list. And that's this point, okay? That's this point. I'm going to, I'm going to clear this real quick when I, I'm going to select this and then select all I'm going to clear it real quick. So if we get down to the point of three, two, and one, right, basically this, it will boil down to two nodes at the end of this recursive call. At the end of this recursive call, this will boil down to two nodes. And lists next, this three, if I want to reverse three and two, how would I do this? I'll explain it again. Currently the head of the list is three. Three's next, next is going to point to three to itself. The reason why is because I want to set twos next to point to three. So then this becomes null and then threes next, I'm going to point to null, okay? That way now it's reversed. Now two is in front of three and three threes next is going to be null, okay? So I'm going to go back again and explain what I'm doing. Three's next is two. This twos next, I don't want it to point to one. I don't want it anymore. I want this two to point to three, right, to the head of it. And I want threes next to point to null. So in the end of this iteration, when we return two, it will be two, which is going to point to three, okay? That's the end of the list, okay? And then while we're doing this, we're recursively calling this function over and over again, while doing this same thing, reversing the links of the link list, every two of them. And then we're recursively calling again, and recursively doing this again, and doing it again and again and again, until we get to the reversal of the link list. And yeah, this code passes all the test cases. And that's basically just of how to reverse a link list. I hope you guys enjoyed this video. This video is pretty tough to explain. I know it's hard to wrap your head around this, but yeah, bear with me. Yeah, rate, com, subscribe. I'll check you guys later. Peace.