 Sorry guys, I haven't been uploading in a long, long time, but now I am. You are going to start uploading every single fucking day. Every single day, like GaryVee. I watched a ton of GaryVee motivational videos, so I'm going to start uploading every single day. Anyway, back to this video. This video is going to be about in-place linkless reversal. So reversing a linkless. So how does a reversing a linkless work? Very, very easy. Let's look at this diagram that is just going to be put on repeat until I stop it. Essentially, we have a previous node that points to null currently. It's just a null. And then we have our current node that's going to go through the whole linkless. As we're going through the whole linkless, we're going to set our current's next to point to the previous node. And then we're going to keep going down the linkless. What's going to happen is that every time you go down the linkless, the current's next is going to point to the previous node. We're reversing the linkless. The values in the linkless are going to get pointed to whatever was previously. So in the end, all we have to do is set our new head node equal to the previous node in the end. And that will be our answer. So this is actually very, very easy. It's not very difficult to understand. If you look at the diagram, we're just going to have our previous node and a current node. And as we're going down the linkless, where current's next is going to point to the previous. And we're going to start updating the nodes as we're going down the linkless. So then when we go to the next iteration, previous is now going to equal to current, and current's going to go to the current's next, and so on and so forth until we go down the linkless. Okay, so this is what the code looks like in C++. So here we have a node called current, we point it to the head, and the previous is currently set to null. And here what they have is they have the next element after current. They have that as a dummy node, because in place when we're going down the list, we have to like, we need to go to the next node, right? And then as we're setting our values, we need a dummy node to store what's next. So here's what we're doing, okay? While the current is not equal to null, right? We're going to go down the list, all right? We're going to go down the list, right? We set our current equal to the head in the beginning, right? And now we're going to go down the list, okay? So now we're going to use our dummy node next, and that's going to point to the current's next, right? That's going to point to the current's next. And now what we're doing is we're going to reverse the current's node pointer. So current's next is going to point to the previous, and then now what we're going to do after we set current's next point to previous, previous is going to equal to current, and current is going to equal to the next. Like I said before, next is just a current's next node, and here in the middle here. And that's what it is in order to go down the link list, right? We're just using that to make sure we have the next node and that we're not going to have any confusion. After we read out all the links going down the link list, previous is going to be all the way at the end, right? Previous is going to be at the end, and because we are reversing the link list, right, that end value of our previous is going to be our new head. So what we're doing is we're setting our head to now equal to the last value of previous as we're going down the link list, because in the end, previous is going to equal to the last value of the link list. So that's what we do here, okay? So our new head is going to point to previous, because while we're going down the while loop, previous is going to be in the end, it's going to be in the end, and that's going to be our new starting value of our head. Okay, so now we're going to go over another problem, okay? So this time, reverse link list two, which is basically reversing the sub list. So given the head of the link list, we are now given two positions, left and right. What we're going to do is we're going to reverse all the values in the positions between left and right inclusive, okay? The positions are indexed by one, by the way. So here we have one, two, three, four, five, right? If left and right are positions two and four, all the values between two and four, including them, get reversed. So here, originally it was two, three, and four, now they're reversed, now they become four, three, and two. So all the values between two and four, in positions two and four, are now reversed to become four, three, and two, and the rest of the list stays the same. So one stays the same and five stays the same between the original. So as you can see here, nothing else changed besides the values of between two and four, the positions from left to right. Okay, so how do you do this problem? So essentially what we need to do is we need to isolate the parts that we are, we're going to reverse. And then once we isolate those parts and reverse it, we could reattach the rest of the nodes in the link list together, and then we could return it. Right, so if this is our positions of start is here and end is here, right between two and four, what we're gonna do is we're gonna maintain four pointers. So here is what we're gonna do. So we have, in the solution here, we have revs, which is gonna be the location of where we're reversing. So here is gonna be revs, this is gonna be revs, and we're gonna have revs end. So that's, revs end is gonna be here, okay? It's gonna be pointers pointing to where start and end are at, okay? So bear with me. And then we're gonna have other two pointers that are gonna be revs previous, right here, revs previous. And then we're also gonna have revs end next. So revs end next, okay? Revs end next is gonna be the node right after revs end. And revs previous is gonna be the node before revs, okay? So as you can see here, essentially what we're gonna do is we're gonna isolate these nodes and we're gonna call exactly our reverse function, reverse this, and then we're gonna set our reverse previous is gonna point to this new one and then we're gonna, revs end next is gonna reattach to whatever value is here, okay? So yeah, that's essentially what we're doing. So basically if you were to do all this stuff and then you call reverse on revs here, it'll reverse this link list using our reverse function. So it's gonna be reverse to four, point to three, point to two. And then we could just reattach our revs previous to these new nodes, have to call the reverse function and get rid of this. And then we would have our reverse values of one, four, three, two, and five, okay? So yeah, that's essentially what we're gonna try to do. So how do we do that? How do we do that? So here, I'll just show you guys a code, how this works. So here, reverse revs is gonna point to null currently and revs previous is gonna be point to null. Revs end is gonna point to null and revs end next is gonna point to null. So what we need to do first is we need to get to the positions of where start and end is. So where start and end is, so we're given the start and end positions of what we're trying to reverse. We need to get to these positions and then we could set our pointers to the right spot, these pointers to the right spot. So this is very easy. We start i equal to one because we're indexing from one and we're gonna loop to the end, okay? And how do we do that? We set a current node is gonna point to head and then while current is not equal to null and i is less than or equal to end. So end is gonna be our end index. So here in this solution here, end is end and start is m. So here, I don't know why they did these terrible variable names, but the starting position is gonna equal to the index m and the ending position is equal to the index end. So while the current is not equal to null and i is less than or equal to end, so i is less than or equal to this last looping up to here, up to end, right? The value of end. What we're gonna do is we're gonna do a few cases. So if i is less than the start, which is m, if i is less than m, revs previous is gonna equal to the current. So revs previous is gonna equal to current. So that's what it's doing. Basically revs previous is gonna keep updating to the current node and while it's less than the less than m, okay? Less than n, revs previous is gonna equal to the current node. And basically this current is gonna keep going down to this link list. So basically we have like a current node and it's gonna keep going down. We're gonna loop to the down to the end of the link list. So yeah, if i is less than m, revs previous is gonna equal to the current. So what that does is that just makes sure is that this revs previous here is gonna be right before revs, okay? Right before our m index. So that's what it's gonna be there for. If i is equal to m, that means we have to set our revs pointer here, our revs pointer node to equal to the current node, the current node that we're looping down from, okay? And if i is equal to n, right? If i is equal to n, so let's say we reach to this end index, n, we need to set revs n to equal to that. So yeah, revs n equals the current and revs n next is gonna equal to current's next. So revs n next, so this revs n next is gonna equal to current's next. So we just set the right pointers to the right values before it. All right, otherwise what we're gonna do is we're just gonna keep going to the next value until we loop to the end of the linkless. So this current is gonna keep going to the next value until we loop to the end of the linkless and we're gonna increment our i++, okay? Now when we get to here, what we're gonna do here, let's say we already set all our pointers up properly here, right, we need to isolate this location here that we're gonna reverse. So what they do here is they set rev n next point to null. So what that does is it's gonna break this pointer and this is gonna point to null, okay? And once they do that, we're gonna call reverse on revs. So revs here points to this part of the starting value that we're reversing and if we call reverse on this node, it's gonna return our, a new function of four, three, two, new values of that. Essentially is that rev end is gonna point to this. So now our rev end here revs end, yeah revs end is gonna now point to that. So this revs end is gonna point to this, the new value that was returned from here. So this is now revs end, okay? So we just reverse this, all righty, okay? If revs previous is not null, then we're gonna set revs previous next point to rev end. So rev end is the new list of reverse values. This revs previous is now gonna point to, it's next is gonna point to revs end. So we're gonna have this, one is next is gonna point to four, three, two, right? So that's gonna fix this part. And then revs next is gonna point to revs end next. So revs end, next revs end, revs next, okay? So revs after it's all been reversed, right? Revs points to this and the revs points to this, after it was all reversed, right? So revs end points to here and revs points to here. So now revs next here, see revs next is gonna point to revs end next. So this is gonna point to there, okay? So this is gonna point to there. So then now in the end we have one, four, three, two, five. So a new low is gonna be one, four, three, two and five. That's what our new link list would be, okay? So I hope you guys enjoyed this explanation. Oh yeah, and then we have this corner case, if the starting and ending are the same positions, then we do nothing, right? Because if we're trying to reverse values between three to three, well, you're not really reversing anything. So all you do is just return head. So that's the starting corner case. But yeah, hope you guys enjoyed this part of the solution and I hope I explained it properly and we're gonna go, okay, we're gonna go with another problem. Given a head of a link list, rotate the list by K spaces. So what does that mean? If I have one, two, three, four, five in my link list, the one, the starting values are gonna go to the right by one. So this one goes to the next one. This two gets rotated right by one. It goes here, the three goes to the right by one. Goes here now and the four originals here is gonna go right by one and go to the last value. The last value of five now gets pushed all the way back to the front to the first value. So now we have five, one, two, three, four. And that's one rotation. Now, they wanna rotate it K times. So K, since in this case K is equal to two, we're gonna rotate it again. So this four is gonna now go to the back to the front. This five goes to the right by one. This one goes to the right. Two goes to the right by one. Three goes to the right by one. So now we have four, five, one, two, three and K is equal to two. So K is moved right by two places. So that's the answer. So yeah, that's just this problem and now we're gonna go over the solution. Okay, so how do you do this problem? It's not that difficult. So basically we're just gonna keep going down and then we're gonna create like a circular linkless by reconnecting it to the front. And then we need to go back to the loop, go back and then we need to split it at the new head of where it's gonna be at and get the new tail. So that's what this diagram shows. So here it's going here and it's gonna go down all to the end and it's gonna like loop it back, close the root ring and then it's gonna go to the new head in this case would be four and it's gonna like split the end at four instead of null and then have a new head. So the code for that, to get to the new head they use this equation, N minus K mod by N. It's kind of strange how they do this this way but it is what it is. So yeah, here we have here the two cases where if the heads and all just returned all that's just special cases. If heads and X is equal to null just return the head, right? Cause that's just one node. Here old tail is gonna equal to head and here what they're gonna do is they set like the number of nodes in the list to find it. So they set N is equal to one and then they check while O tail is next not equal to null, N plus plus and they just keep going next on old tail and then in the end they set O tail is next to point to head. So at this point in the end of this what it's doing is that it's just closing the loop. So it goes through, finds calculates the number of nodes there are and then it closed the loop to set it back to the head and then now they're gonna restart looping it through the new tail is gonna equal to head and they use this math equation if you just try multiple cases and see when you could calculate when the next for the new tail where it's gonna end at it's this equation N minus K mod by N minus one. So that's what they did here and then they just said new tail is equal to new tails next and after that they point the right spot for the new head is gonna equal to new tails next and then they break the tail so new tails next is gonna equal to null and then they return the new head. So yeah, essentially close the loop go use the math equation to find where it's gonna end at so here ends at three they're gonna set the new tails next to become null and then return the new head to where it is. Yeah, so the new heads equal to new tails next send new tails next equal null and then return new head. So yeah, that's just in this code you could write this in C plus plus also but yeah, it's not really similar to, you know, like reverse single link list but it's kind of sort of similar. Yeah, it's not exactly similar but it's very sort of similar. All right guys, the next problem that we're gonna go over actually final problem we're gonna go over is reverse nodes and K groups. So this time we're given the link list and what we're gonna do is we're gonna reverse K nodes at a time. And if we reach the part where it's not a multiple of K then we just leave it remain as it is. So in this case we have one, two, three, four, five and K is equal to two. So every two nodes we're gonna reverse it. So you see this one two, this one two becomes two one, it's reversed. Right, so that's the first two and the next two is three four and then reverse it to become four three. So that's reversed also. And then five, at this point, the next two, there is no next two so that we just leave it as it is and we leave five there by itself. In this case, the next example one, two, three, four, five K is equal to three. We cut every three. We're gonna reverse every three. So in this case we have one, two, three and this is gonna get reversed because there's three nodes and it becomes three two one, okay? And then four or five afterwards there's no third value so it's not a multiple of three. So we just leave it as it is and leave it four, five, okay? So that's just this problem and let's go over the solution now. So the solution of this problem is it's also assuming we have the reverse function as before. We still need the four pointers now. Head, which is pointing to the original head of the next set of K nodes. Reverse head, which is basically the tail of the original set of K nodes. K tail is the tail of the previous set of K nodes. After reverses so this is gonna point to the previous set of K nodes. New head is the new head of the final list that we return as the output. So it's the K node from the beginning of the list. Okay, so basically the same algorithm remains as before. The only difference is that we count K nodes and we're able to at least get K nodes to reverse them and get our new reverse head and then we need to see if the K tail is set or not. So if it's not set, then we're gonna set it to, if it is set then we need to attach the next value, the K tail is next to point to the reverse head and update the K tail to point to the tail of the reverse set of K nodes. And then our K tail is gonna equal their head. So we're gonna keep doing this until we reach the end of the list. Okay, so let's say we have this, one, two, three, four, five and K is equal to two. We first find the two nodes that we need to reverse and since we have at least these two nodes we're gonna reverse them. So we reverse them and now we have a reverse head is gonna equal to this and then K tail is equal to this and then head is equal to this. The K tail wasn't set. Now, it wasn't set before now points to the tail of the reverse set of K nodes. So it's gonna equal to the tail of the reverse set of K nodes. The reversing severs a connection, yeah. So the reversal function severs a connection between the rest of the list. And now what we're gonna do is we're gonna set our new head. We're gonna find two values of the nodes and then we're going to reverse these two next two values and then it reverses it. So now we're gonna have reverse head and regular head. Right, now they're reversed. And then at this point we need to connect K tail to point to next point to reverse head so we connect these nodes together. And then this last value is also in our reverse function it gets severed so we need to reattach those values last part also. So yeah, here's the code. It's not that difficult. So here we originally have the same reverse link list. Kind of similar to what we had before. Their version is that they start K is greater than zero and then do their thing here. They're decrementing the number of nodes reversed. Well before in our code we were like incrementing. But anyway that's the reverse link list. So here, so this was reversed link list of K. So here reverse group. So this is the gist of what our code does. So we have a point of point to head, K tail is null. The new head is equal to null. And we're gonna keep going as long as there's nodes in the list. So we have a count variable. We're gonna start counting from the head. Yeah, so while we're counting the variable until we reach K, we're gonna get our nodes, right? So the point is gonna keep going next until we reach K nodes. So now we've reached K, we're gonna reverse the K nodes. We're passing it into a reverse function. If the new head is ahead of the final list, our new head is gonna equal to reverse head. So our new head here. So if our new head is equal to the final list, our new head is gonna reverse head. So reverse head is pointing to this now, right? And then our new head is gonna equal to reverse head, right? So that's if it's not equal to null. So if the new head is ahead of the final list, so yeah, we just had a new head is equal to reverse head. If tail is the tail of the previous block of reverse K nodes, we need to set the K tail is next equal to reverse head. So this is checking if there's nothing left at the end. So if there's nothing left at the end, we set our K tails next, it's gonna point to reverse head, right? Point to reverse head. So then we recombined every, every two times we split these link lists, we're gonna reconnect them together. So K tails next is gonna point to reverse head. So that's that. Yeah, so it's not equal to null. K pros next points to reverse head. And then K tail is gonna equal to head. So K tail is gonna equal to head. So K tail is gonna point to a head. So this is gonna go to the next K values at the end. So it goes to here, right? So it goes to head. So this K tail then becomes head. So it goes up to here. And head is gonna equal to pointer. So head is gonna equal to pointer. So pointer is, it's pointer is what we were traversing through, right? So yeah, pointer is gonna equal to pointer is equal. Head is gonna equal to pointer. Okay, so then our new heads gets put at what we're traversing through. So yeah, so that's that. And then if we reach the end of the portion, the K tails next is gonna equal to head. So K tails next is gonna equal to head. So this, so because when we reverse things, our head ends up at the end, right? So what they're saying is K tails next is gonna point to heads in order to recombine the last part of the rest of the link list. So yeah, that's this part. We reached the final unreversed portion. Okay, then if the new head is equal to null, we're gonna return the head. Otherwise, we're gonna return new head. So this is just checking if in the end, if new head is equal to null. So if we reached like a null value here, this was like originally empty and we're just gonna return the head here. At the last byte, otherwise, we're just going to return new head, which is our new link list pointer. That's where we're connecting it to. So yeah, that's just of this code. This part is a little bit more confusing. But essentially what we're doing is we're gonna go through every K values and then we're gonna reorder them using the reverse method. And then after we reverse it, we just go to the next one and then reorder it again, redo all the links again, and so on and so forth until we get to the end. So yeah. But yeah, that's the total gist of this problem. I hope you guys enjoyed this video. Write a comment, subscribe. I'll check you guys later.