 What's up guys, my name is Michael and welcome to my YouTube channel today We are going to go over how to reverse a doubly link list. So this is a little Little bit tricky. It's basically the same thing as reversing a single a link list So we have this list of one two three four and four is next points to null, right? Now after we reverse the link list now, it's going to be four three two one So it's like all of the values here are flipped So Yeah, I'm just gonna go over the code how you do this. It's basically the same thing But now we have more links. We have to change so what we're gonna do is we're gonna recursively call this function over and over again and We're just gonna do some Parameter links changing. So the first thing you want to realize is that when we're doing this recursive call We need to get the base case. So what is the base case? The base case is when You reach the end of the list. So if I reach the end of the list And say at four, I'm gonna have to return four, right? I'm gonna return itself because this four is gonna become the new head, right? I want to make this for to become the new head so that for When I reach the end four is gonna be the starting node and it's gonna point three. It's gonna point two point two one So when I reach this end of four, I want to return itself Right return itself, but there's also another case in which if I reach a null pointer So let's say I reached this point null then I also want to return itself because this means it's it's the end of the list So that's why I have that here also. So if the head is equal to null or the heads next is equal to null We return head because that means we return all And it also means we return the current. Yeah, we just return all Okay, now this part is a little difficult to understand What I'm gonna do is I'm gonna recursively called this method reverse over and over again passing in the next node passing in the next node now Let's think about this if I'm recursively calling this over and over again, and I want to change My new head to instead point to Instead of having it starting at one. I want to start at four now So what would I do? Well simple when I reach the end of this node for I want to return it All right, I want to return this four. So then this four is now our new node So that's why in this part of the code at the end This current I shouldn't rename current to new new head Yeah This new head That we call reverse over and over again, we're gonna return this new head So now this new head is now our new Starting value of our note after calling recursively call again Now this part of the link changing is a little difficult to understand but I'll try to explain it to you Let's say we have the list of this list one two three four. Okay, and Let's say we're cursively calling it over and over again, and then we reach this point of the last two values of three pointing to four and Four is previous pointing to three, right? Now I want to change this so that in the end I get like what I want to get is I want to get four to point to three and Three is previous to point to four Right, so I want to get to this this configuration because that's what the values are reversed, right? So what I do here is that Currently the head is three So what I said is three is previous is now going to point to Three's next so you had it is currently three. So what I'm saying here is that this three's previous value here, right? This previous value Right before it was like two now this previous value is going to point to threes next which is going to be four This previous value is going to point to four Yeah, I'll do it this way a point to four, okay, let's assume this four is the same as this four Which it is in memory, but I'm going to show you like the same duplicate Duplicate four is for now to make it sure you guys understand what's going on So threes previous is going to point to threes next which is four see it's going to point to four Now what I'm going to do is I'm going to set threes next next it's going to point to three So what does this mean this means threes next is four Right threes next is four and I want to set four is next to point to three So what this is doing is it's going to set threes next next which is going to be four Four's next is going to point to itself, which is three. So that's what I do here right head right now is pointing to three so Head is three currently so three's next next is four and we're going to set this to next to point to three So this means that four is next so threes next is four this four is next. So let's get rid of this This four is going to actually point to three right and then Yeah, that's what this line is and then now what we're going to do is we're going to set threes next previous So this threes next This four is a previous We're going to set to null pointer. So threes next is four So four is previous is going to now point to null Right, so then this is gone And then we're going to set threes next is going to point to null. So then this threes is going to now be gone threes next is going to point to null and Then our final configuration is this we have four is going to point three threes previous is going to point to four so that's what The link changing does okay, that's basically the gist of how this is a turning out and Yeah, it's a little smiley face here also if you look at it But yeah, that's basically the gist of how Reversing the link list is like and again, we just return our new head and yeah, that's pretty much the gist of it Hope you guys know this video rate com subscribe. I'll check you guys later. Peace