 Hello, welcome back. This is Christian. In this lesson, we're going to implement the function to insert a new node before an existing node in a singly-ling list using Python. So here's an example. Let's say if you want to answer the frog in front of the mouse, okay? So typically the way you do is you create two nodes, one to point to the current node as you traverse down the list. And then we have another node called previous or previous node that points to the previous node. So these two nodes will be incremented sequentially down the list until the current node finds the matching key. Once you find the key, then we want to do two operations. First, we want to set the current new node, which is the new node here. The next pointer is going to be assigned with the current node. So it points to this current node. And then we're going to point the previous next node to the current node. And so in this way, we're actually breaking this bond, this link right here, okay? If you don't do that, you might break the bond. Now, of course, some people might like to do just with one additional node where you can check for the next dot next as the current node, and then you don't have to have an extra node here. But I mean, that's okay. You can do it in several ways. I like to do this way because you have two nodes to deal with and you always focus on the current node, okay? So this is one case where you insert a node between two nodes. There's another case where the first node might be the key. So if that's the case, then we have to do a prepend. So the process is the same as a prepend. And I think I have a diagram for this as well. So yeah, it'll be like this. Let's say that the target is the dog and since it's the first node, then we want to do the operation where it's similar to, kind of similar to before. We want to assign the new nodes next pointer to the current node. In this case, it's the head. And then since this is the head, we want to update the head node to point to the new node. So the new node will come, it's new head. If you don't do the update, you've got to get the head pointing to the second node and that's incorrect, okay? A little bit tricky, but, you know, lots of trials and errors we asked you to, but let's go and see how this is done in Python. Okay, so back here on the IDE, here's a beautiful list of all these emojis here. Okay, I'm going to close this window over here, a little bit too many panels. All right, okay, so let's, let me let me close this one now. It's a little bit distracting. Okay, let's go to our list over here and we're going to insert a node, a function right below here. Maybe right here, we call it insert before key. We will take this self, the key and the new item to be inserted. And then we're going to say, again, a base case itself.head is no or none. They don't return nothing, okay? So that's, we can skip that. Again, you don't need to do this, but it's kind of common because I want to check down here anyway, right? Because I check in the loop anyway. Anyway, so if you notice in the previous one, when you insert after, notice we sent the key to a search function up here and the search function will return the node matching that particular key, right? And then we just sort of after that. In this case, we can't do that because you know, even if you found the current key, we still need to know what the previous node is, right? So in a way, we have to kind of do this manually ourselves, which is not too difficult. So first let's create a new node. It's going to be assigned to the new node instance of the node class. And then we're going to create two additional nodes to the current node. This is assigned with the head node up on the head. And then we have a previous node, we'll be pointing to none yet, okay? It's not assigned yet. So the idea is that we're going to traverse the list. So we'll say while the current node is not equal to null or none. That means it's not the end of the list yet. Then right away, you want to do this. We're going to do a previous node. This is going to be assigned to the current node. And then the current node is going to be a point assigned to with the next node, which is the current.next, okay? So you make sure you do that before you forget because that can cause your code to be infinite loop. So what about here? Then we need to check to see if the current node is the key, right? If the current node, the item is indeed the key. That's so that we found it. And then two things can happen, right? So the first thing is, of course, we want to set the new nodes next right away to the current node, okay? And then now we have the two special cases. If the current node is the head node, that means that we have to assign then the head to this current node. Otherwise, we just call the previous.next assigned to the new node, right? So we're to special case. So here I put here, you know, insert before head or something like that, okay? So that means that if the previous node, how do we know this? If the previous node is none, that means that we have never, you know, touched the previous node at all. So as we run it for the very first time, it matches the first node already and we found it. So if that's the case, then the previous node will always be null, right? Otherwise, we would have set it down here already. So it's not true, okay? If that's the case, then we actually, what did I do? We found it, we just set itself down head to the new node and then we're done. Else, that means we're gonna move to the previous node. And it'll be the previous node.next assigned with the new node, okay? And that's it, right? Otherwise, if it's the head node that the head doesn't have, if the head is indeed null, yeah, if you do it incorrectly, you're gonna get, yeah, it's gonna be, it's gonna be correct. So we want to make sure we do it correctly. Okay, and then finally down here, make sure we update this self that size one. And then we can just do a return here is one. Okay, we return or we can do a break to break out of the loop, doesn't really matter. Some people don't like too many return statements in the function, some don't mind. You know, some people prefer just to have one return statements in function. That's something that you can debate at a different time. Some people like to have a very pure object oriented approach where there's only one return statements. I don't know, I'm not a pure, so whichever is convenient, which is best, you can use that, okay? But it is, can be very confusing if you have a lot of return statements. So I mean, don't overdo it. And we're simple one like this, it's fine. Okay, so if that's the case, then I think we should be good to go. So let's give it a test and see if it works. Let's go over here and let's say that one, I wanted to copy this right below here. Alice is growing. And so I wanna print, maybe I put a message here. I'll do insert after. Okay, maybe I should do this as well up here, you know, this here, do insert before. I should do this after as well, right? Not sure why I have two of these, what do I have? Oh, I'm sorry, okay, the tail and the head, okay, that's fine. So focus on this one here, okay, this is, this is gonna be before, so before. Okay, so I wanna pick a different animal or they got the lion. So I have my emojis right here, you wanna look at it. This is the site I used to get all the emojis from, getemoji.com, very beautiful site. So I'm gonna use the rooster select here. Okay, you rooster, you have the monkey. Let's go with, let's go with the rooster. It looks pretty good to me. And so we're gonna replace the lion with the rooster. And I'm gonna insert before, let's go before the cat, okay? So before the cat, all right? So let's see what this looks like. And let's give it a try. Oh, here we go. Let me make this a little bit taller over here. All right, so here we have, this is the one right here, okay? The previous list looks like this. This is just the reverse, right? So the list for this function like 23 is actually this one here. So we have the cat right here and we're gonna insert before the cat. So as you can see the new list down here, there's a cat and here's the rooster before the cat. Okay, so it looked good. All right, so let's do another one. This time we're gonna print, again, make sure we test that, test this before the head, okay? So the head is the frog. So let me copy the frog, put it down here. And let's pick another animal before the frog. Let's pick a, I don't know, a lobster. That looks good to me. And the lobster goes before the frog. So lobster should be number one. And here we go. And there it is down here. It's kind of hard to see, but you can see it's down here. Okay, maybe I should clear this. Sorry, one more time. Boom, there it is. All right, so here's the frog and before the lobster is indeed now the fun. Excellent. All right, so this is the implementation again. It really looked very, you know, not that difficult. Just be careful when you insert before the head, you have to make sure that you update the head and otherwise you don't lose, otherwise you lose the head as the pointer. All right, so in the next video, we're going to do a delete and we'll delete the note at the tail of the list or at the head. I don't know which one yet, but we'll do one of those. Thank you.