 Hello everyone. Welcome back. This is Christian. In this video we're going to insert a new node after the current node. So this operation requires that we traverse the list starting from the head. And once we find the node that we want to replace after, then we'll do this operation. So the curve pointer here points to the current node that matches the key that we want to insert after. So let's say we're looking for a cat and we want to replace the frog after the cat. Then this is the operation that takes place. So first we need to set the new nodes next pointer to the current next pointer so we don't lose the bond here. And this is going to be set to the mouse in this case. And then we're going to go back and change the current next pointer, update that to point to the current node. And that's it. So let's go and see how this is done in Python. All right. So back on the ID here. Now I'm going to go and to the link list in here and create a function. Now before we do that though, it's going to be very similar to how we do the print up here. We have to traverse the list. But let's also return create a function called search because we have to search for a particular node in that list. Like we search for the cat. So let's do that. Do a function called search. Search, oops, not that one. Search for a key or particular node. And this is the key or search for. And this function will return to know that it finds. If it doesn't find anything, if it's not in the list, it's just going to return no value. All right. So let's do the following. So again, we need to set the new node. Watch the current node. It's equal to the head. Okay. And then we'll do like while current node equal to null none. There we go and we traverse the list. Okay. So if you find the list, if the current node is indeed the current node item is equal to the key we're looking for, then we found the node and we just basically return the current node. Okay. If it's not there, then we'll, if it's not, find it and keep going. So update the current node to the next node. And if it's not on the list at all and they're written down here, we just return a none. Okay. It's a very simple search algorithm. Okay. So now, so once you have that function, then we can go ahead and do the insert. So we do insert after a key. We'll have to know, I guess, doesn't matter. We need the key that we need to look for. And then the new item we want to search and replace. I mean, insert it into, right? So here we have, so again, you can always check to see if the head is null. If so, head is null, right? None. Then just return none. We'll just skip out together. But of course, again, I already showed you before, you don't need to do this if you want to, because it's kind of redundant. Okay. But I will just leave that for now. So we'll need to have the new node is equal to the set a new node of the item. And then we need to get the current node is going to be assigned to a search function. Okay. So we're going to search the self that search key. And then we just pass in the key of the item. And then we'll come back as the node. If the current node is not no, then we know for sure that we reach the, we find the key. Otherwise, is nothing happened, right? So we say, if current node, oops, it's not equal to null. We know that we found the current node already. And we'll just do the operation. Okay. So the idea is then we want to make sure that I have to take your two things because when we insert after, it's possible that it might be the last node, which is a tail. So we have to be careful with that. If that's the case, then we want to also update the tail to be the new node, right? Otherwise, you could just skip that. So if this is not no, and then we're going to go ahead and put the new node first, want to get the, the next pointer points to the current node that next. So again, so if you find looking for the cat, right, we insert between the cat here, for example, if the cat is found, then we want to insert the new node after the cat, which is in this case, the mouse. We want to point the new ones, the new nodes to the mouse. And then we can go back and then change the current node that next point to the new node. Okay. So be careful how you do this. Again, the order is important. If you're doing correctly, it's kind of, it's not going to work. So that is it, right? But I also want to check for the tail node. If it's a tail node or not. So how do we know if it's tail node? Well, let's say that we found the cat. It's at the end here, right? So how do we know this is the tail node? Well, you can check the next node if it's no or not. If it's none or no, do we know that this is a tail node? If that's the case, we just want to update the tail to the current new node. Okay. So here is a tail. So we say, put here, maybe a note here, say, new tail node. What is a new tail? Yeah, no, it's fine. So if the current node next is equal to none, then we have to put self that tail is equal to the new node. And that's it. Yeah, I think that's it. And then, yeah, that's, I think that's about it. Let's give it a try, right? Actually, the key here, I actually made a mistake here. Now, this is not that, when we search, not the item is the key. And yeah, we search the key, not the item, okay? The key is the item we want to search for. So let's give it a try, save. And I'm going to go in and insert, let's do it right here, that's list, insert, after the key of which one we're supposed, yeah, let's insert right here. This is the list we're looking at, okay? Another reverse one, just this list here, we want to insert after the cat, we want to copy the cat. This is the key. And then, I want to insert a new emoji. Let's go and get a, I don't know, maybe a Q piggy here. So you put it after the cat, okay? And then if you know for sure if it's correct, we can print it out. Hopefully, it's there. So let's give it a try. And there's too many of these. Let's turn off the reverse here. Well, yeah, we don't want to, we want to make sure that you do want to make sure that the reverse is also true because if it's not correctly linked, it is going to be in a problem. But I'm going to put here a print statement so we have, you know, some spaces in between and we should do that. Let me see here. All right, so here we go, cross your fingers. And we got an error, it's perfect. Let's see what I do here. There's something, let's give me a second, I can't see down here, attribute next at the current node at, let's see current, oh, mistyped to the next right here, you can see it, typo. Where is it at? Right here. Great. All right. So we go again, let's clear this list over here and do another run. So here we go. We go in here and again, this is the list, this is the one, the previous list, right? This is the reverse. And then we just replace the pig right in here and then we're good to go. So that's how you do it. All right. So now let's do one more test. We want to make sure that it's working at the tail. So let's do another insert. This time, I'm going to copy this again. And we will insert at the very end. This case will be the mouse, right? So the mouse is the tail, it's pretty here. And let's go to insert a, I don't know, let's go with the lion. The lion is pretty cool. Put the lion at the end. Okay, so it'll be the very last one. And let's give it a try. And here we go. This is the end of the list. It's the before list. We added the pig right after the cat. And then we added the lion at the end of the list at the mouse. And here is the lion. Perfect. All right. So this is the algorithm for that. Of course, I use the search here just to handle the search part. Otherwise, you would have to basically put this whole thing here inside here to get the key and so forth. Okay. So the next video, we're going to do a nice insertion. But this time we're going to insert before the current key. And that is a little bit tricky. So I'll leave that for another video. So I'll see you in the next video.