 Hello, welcome back. It's just Christian. So in this lesson, we're going to learn how to remove the head node and the tail node It's a little bit tricky. So I'm gonna go over that first. So here is an example where we move the head node all we have to do is basically do one operation and You know update the head next actually after the head to the next head next and basically that's it However, there's a case where maybe both the head and the tail are pointing to a single node If that's the case then you know when we wouldn't in that case We just basically set both the head and the tail nodes to none. Okay, we move the tail note However, a little bit tricky as well. Right here is another diagram here when we do that. We have to track the Again, there are a couple ways to do this Okay, so I mentioned my previous video that usually you will use two pointers One for the current and the other one for the previous so that when you reach the end If the current node is pointing to the last node, then you can you know Assign the tail to the previous node, which is a perfectly fine And the other meant that was I had said that you can also use a single node Pointer to point and you can check the dot next on next if this is the last node And then we found that this is the tail already. So we just update the tail node to the current node Okay, so I want to do this time using a singular single Node here. All right, so let's go and see how this is done Now before continue, I think I discovered a bug and one of the previous videos in the Insert after function. There is a bug. I think right here when we update the tail node Okay, so notice that when we update the tail node, we updated that the current node is Assigned to the current next node, right? And then the next Next is pointing to the current node But then the the tail know which I if we would check if the tail know is correct or not And so if I say if the current that next is none in this case It's not true because we already assigned the current that next to the new node So therefore this will never be none, right? Because it's not true So it will never update. So in this case, it's not true What I want to do here is that if not the current node, but it's be the new node Okay, because we're sending new node The next to the current's next node So the next note could be empty it could be not But if it is empty that we know that the next node is the current is going to be the tail No, so therefore we update the tail node. Okay, so make sure you fix that on line 67 or in the answer after key when we check the tail node Make sure it says new node next and not current node next or you can say current node next next then that's correct Okay, so just one here. They want to fix it. All right. So now let's do a Remove of the head know first that is the user approach So again, if you want to remove the head node, let's say the lobster here for example, then Basically, we just check to see if this empty or not. So base case, right? So here we're going to put it define a function called remove head And we have to just say yourself the head is indeed none then return nothing Right, just return. Okay. If that is not the case then we want to go ahead and Check if the both heads are the same or not. So you can say if the self head is equal to the self tail If that's the case, okay, then again, we just basically set the head to none and then update the tail as well If you don't do that if just after the head and the tail we be pointing to something else. Okay, if If they are and that's not the case, then we know that it's else close right I mean that the head has one node and then the tail has at least a different notes at least two notes because they're not the same. So before it's very simple. Just put the head To it's Next note. And that's about it. And then don't forget to decrement the cell size by one Right. So let's test that first. Make sure we it works for you. Go on. So go back here again and I'm going to go down here and copy this We'll put it up below here and I want to do a, we'll just say remove Okay. Okay. This time I'm going to move the head, which is the lobster. So we're going to move the lobster. Just one key. And if that's correct, the lobster should be removed. So let's let's kill this over here and start a fresh one. So oops. Item. Oh, I forgot to do something. Maybe the self remove Excuse me. What I do here. Answer before Missing more position item. Let's see. Do something here. Where is it? Doesn't tell me where it is. On insert Before missing one. Okay. Answer before Insert before. Let's go back. I'll probably remove something over here. Which one is it? Okay. Doesn't tell me where it is. Before Okay. Okay. My mistake should be in my insert. Just be removed head. And we're not. Yeah, it's the wrong function. Sorry about that. Let's clear that again. All right. So here we go. One more time. Okay. Still not working. Hmm. Oh, I don't, we actually, yeah, I don't need anything, right? Yeah, I don't need anything to pass that because we actually basically removed the head. So it'll be the lobster that should be removed. And here we go. All right. Thank you. So here we go. This is the list and we've moved the lobster, which is the head. So now it's gone from that list. All right. So perfect. Now let's do the tail. So down here we kind of similar. We move tail. Make sure forget. Don't forget to put anything to it. Okay. Pass anything to it. So it'll be if self that head is a none. Then return nothing. Right. Again, if the self that head is equal to self that tail, then same case as above. Both of the just removed assigned to none. Not then we know that we have to update the tail and the tail is. So we have to do a traversal. Okay. Because we don't have you and we have extra tail, but you can't know the previous one. Right. So we have to traverse this list. So I want to do a current node only assigned to this head. Not this Java. And then I want to say while the current node. Next. Okay. It's not equal to the tail. If it's not equal to tail, then I'm going to update the current node. The next note. Okay. So what I'm doing is unassigned the current node to the head. So here again at the, at the, you know, lobster right. I look at that. It's, it's not a look at the next node. Right. The current next. Is this the tail? No, it's not. So I move here. Is this the tail? It's not. So until I reach to the rat here, the mouse, the mouse I check the next is the lion and the lion is indeed the tails. If that's the case, then I stopped traversing my current node is still pointing to the mouse. Okay. So in this case, all I have to do is I move the tail to the mouse, which is the current node. And then set the current next pointer to none. So basically we move this note. Okay. So that's I'm going to do. So if I keep doing that until I find that the next note is indeed the tail, then I'm out of the loop. And then here I'll just do this self that tail is going to be assigned to the current node. That is the new tail. And then the current node that next is equal to none. Okay. And then finally, out here, we just have to basically again update the size. So we'll see if this is correct. I think that should be the case, but we'll see. All right. So let's say this and let's go here again. So we already moved ahead. We moved the lobster. The tail is indeed the lion. Okay. So I'm going to type the line over here. And we will put right below here. And we do remove tail. Yeah, this should be the tail. Put a comment here. This should be the lion. And this should be the lobster. I put it here as you can see. Okay. I'll be moving both of those from the head and the tail. So let's give it a try. So we'll go down at the bottom. We see that we've removed the lobster from the head and we've removed the lion from the tail. Okay. Just to make sure it works again. We can do one more time. And this time we should move the frog and the mouse. Okay, so let's do one more time right below here. And you're probably moved. I should probably say we moved head. Oh, doesn't make sense. This time I'm going to remove the frog right here from the head and the tail is going to be the mouse. So let's see if this is correct. So here we go. Down at the bottom again. Let me clear this. Let's try a new run. Doesn't matter much. So here we go. We remove the lobster first. So that leaves the head for the frog. And the head was the mouse. We removed the frog. We got the dog as the new head. And the pig is now the new tail. So perfect. All right. So you can see a little bit confusing here at first, but you know, not too bad. So again, this is very common, right? So again, you could have another function just to do this again and again. So in the next video, we're going to let we move an item from in between the node.