 What's up guys? My name is Michael and welcome to my YouTube channel. Today I'm going to go over inserting a node into a doubly sorted link list. So it's about the same thing as inserting a node into a sorted singly link list. Basically, we just have to find the position of where the node is supposed to be at. And then we could just reroute the links. So, um, yeah, I'm going to just show you guys how the code and how to do it and explain how it works. Because I feel like you should know like how this would work. Basically, if I have like, only close to one, two, three, four, one, two, four, and then null, right? I want to insert three into this. I just need to find the location where two is at, and then set twos next to become three, then threes previous to become two, and then threes next to become four, and then four previous to become three. So that's basically how you would do this. So yeah, I'll just explain you guys the code. How to do this recursively. So let's say we're given the the method of sorted insert doubly linked list node list and then data. We need to create our new new node first because like how else would you do it if you don't have your new node, right? So for that, I just created a new node, link list, use new doubly linked list node, and I have zero for my data. Then I just I set my data equals to data, which is the data that was passed in. So given the data that was passed in, we just create our new node, new nodes data. Now what I'm going to do is I'm going to set the next. This is a weird syntax that I used. But basically what it's doing is remember assignment is from right to left. So what this is doing is it's creating new nodes previous is going to equal to null. And then new nodes next also equal to null. So both of these aren't going to be null for now. For now, it's going to be null. Like the next rate, the the next node of new node and the previous node and you don't are going to be null. Okay, so if the list is null, right, if our current list has nothing in it, then we're just going to return our new node, because that means that that's just going to be the only element in our list, right? Like if you have only this list, and there's nothing in the list, then we're just going to return our new node, because that's only element. Now what we're going to do is we're going to check if the if the current data that I want to menu nodes data is less than or equal to the head of the first data, right? If it's less than or equal to the head of the list data. That means I want to insert it right before this, right? I want to insert this right before the head. So what I'm going to do is I'm going to have my new node have its next point to the head of the list of the data, right? And then you have the previous of the head of the list point to our new node. So this is just basically just inserting this new node into the front of the link list, right? This inserting into the front link list. So this is the case in our new head of our link list is going to be our new node, because once I inserted my node, and I changed the links, right, my new nodes next is going to equal to the head. And then the list previous is equal to our new node. Then my new node is not the head, right? So then I just return my new node. Now, otherwise, this is like a little confusing. But let's assume that our method actually works. So what we're going to do is we're going to get the rest of the list, and repeatedly passing in next, right, to insert it at the right location. And what I'm going to do is I'm going to create my, my lists next is going to equal to the rest of the sorted list. And then I'm going to have the rest of the sort of list previous to equal to our current list. Then I just return the head of the list. So what this is basically doing is it's recursively calling this function over and over again. And then it's taking the rest of the sorted list and placing our new node in front of it, right? So like this is just making sure like, Hey, if if if I'm recursively calling the rest of the list to insert this new node, that created my new node, it's going to insert the rest of the list to point to the rest from the new node at the head location. So because we're cursively calling this, our list is always going to be sorted and inserted correctly. So yeah, I could explain you guys more clearly. So like, let's say we had like, one, two, and then four, right, and we're inserting three. So what the what this code is doing is basically we're inserting three, and it's going to call recursively call this on the rest of this list. Right. So if three was the right, if it if the right position was if I wanted to insert it after one, which is not what I want to do currently, because that's not where this this list at though. But yeah, what it's going to do is it's going to recursively call on the rest of the list of this. Then the codes going to check, Hey, am I going to insert three right after two? And I am going to insert three after two. So what this does is it takes this part point of this two, it's going to set a point equal to three. And that's pre threes previous to point to two. And it's and it changes threes next is going to point to four. And then this is gone. And then four is previous is going to point to three. So then, yeah, it looks like this. So then in the end, we have one goes to two, which goes to three, which goes to four. Okay, so then we have all these are also connected by previous links. So yeah, that's basically just this code. It's just like what it's doing is like it's checking if you if the needs to insert at the head of the list. And if it does, then it returns the new head of the list. Otherwise, it's going to like recursively call on the rest of the list and then set its right pointers. Set the rest, the next equal to the rest of the previous list and the previous equal to the rest of the list. So yeah, that's just this code. I hope you guys know this video where I can't subscribe. I'm a little tired. That's why I'm a little hard to do this problem. But yeah, peace.