 Sorry guys, I haven't been uploading lately at all because of my large schedule of changes and all this quarantine and then a lot of homework But now I'm back. I'm back to uploading. I'll try to upload every single day, but we'll see what happens. All right So let's go over this topic called link lists from the poll I saw that you guys want me to go over graphs But link lists are actually the most easiest topic to go over really quickly and I also don't have that much time So so what is a link list a link list is basically just well from the name of it It's a list. It's a list of objects and they are arranged by links. Okay, that's what a link list is So let's say I have this object called a I don't know. Let's let's say let's label it as a B and C Okay, so I have a B and C These are just letters in an alphabet and if I insert them into a link list Let's I insert a into the list that's and then I insert B But that does is if I insert a and then I enter B It connects A to B doing a pointer and we call this the dot next pointer right next right It's next is B And if I insert C the B's pointer because B is out of the last element of the link list gets point to C So as you can see here if I insert like a bunch of elements I'm pretty much just adding a link from the last element to the next one that I'm adding So if I insert D, I'm gonna have D. I'm gonna insert it right here. Okay. I have a pointer of the last element I'm gonna point it it's next pointer to D. So that's basically what a link list is you have pointers from every single element The next value is the next pointer and it points to the next element that you're adding in whatever list Okay, okay, so now let's talk about doubly link list So double your link list started the exact same thing as link list. This is doubly link list same thing Let's say I were to add insert a B C D Okay, so if I I'm gonna create an object of a and then I'm going to create another object of B Right the one I'm adding. I'm gonna add a's a a's next pointer to be what a doubly link list is It's exact same thing as a link list except we have another pointer called previous So remember this this is the next this is next right the next pointer of next B is previous is a So we have another pointer for previous so the doubly link list is exact same thing as a link list except all the links We instead of having just a pointer to the next element We also have a pointer to the previous element So in this case, we have a is an X pointer goes to be these previous goes a okay Now let's say I'm gonna add C and then D. Okay, C and D If I add insert C, I'm gonna insert create an object of C and I'm gonna point B's next to C And I'm gonna point C's previous to be right and That's what a double link list is if I insert D until this doubly link list I'm gonna create a link to D and C's next pointer is gonna be D and then these previous is gonna be C Okay, so if I want to access whatever elements there is a let's say D I want I want to link to the previous of D. I do D that previous D dot previous and that'll give me C Okay, so that's how these link lists work. All right So now let's talk about something else. So link list could be sorted or unsorted It really doesn't matter. It depends on like the order you're adding it So like if I inserted like a and then C and then B would it would have been a C B, right? It's not like sorted. It's not a sorted order. Okay. Um, so How so how do you create a link list? So like how do you how do you add values to a link list? So in both of these What people normally do in some implementations of link lists, they have something called the head pointer So let's I call this head and what the head pointer does it's just a pointer that indicates if it's the first element of link list So the head is going to point to the first element of link list, which is this is a right That's the head of the link list in some implementations. There's also a tail the tail would point to the end of the link list Okay, so here sometimes some implementations. We have a tail pointer that points to the end of the link list So that really depends on whatever whichever implementation some people do some people implement it like this simply we'll end up like that. But yeah Um, what you could do if you do have a head pointer is that if the heads null Which means it has no value, right? If this head points to null What does that tell you that tells you that this whole link list is empty, right? Like if this head is pointing to Stop, uh, this head pointer is pointing to like nothing null, right? Let's say this link list null and that means this whole link list is empty, right? Because if you If it has a point to something in order to show that it's there, it's not empty, right? So yeah, if if they're if your head pointer is null, that means your link list is empty. Okay Okay, so now let's talk about searching So sometimes if I want to search for a value in a link list, what do I have to do? Well, um, let's go back to this example This example a b c d Let's say I want to find the value c So how am I going to find the value c like I want to find this value c Well to do this I actually have to start from the beginning and then keep going to the next keep going keep going Until I find the value c right if I reach the end of the list Which is I reach to the d like the end of the list And c is still not found and that means c doesn't exist in the The link list. Okay, so if you were to start at the beginning To search for an element in link list you have to start from the beginning and keep going to the to the next value So let's say I want to find c. I'm gonna start at the beginning a I have to go to the next value of a b is b c. No, it's not All right, I'm going to go find the next value of b Our next value of b is c C is equal to c. Yes, so I'm going to return true. Okay Um, if I reach to the end and I still can't find the value of c Then that means it doesn't exist So the pseudocode for that is going to be I'm going to put it right down right here List search in clrs. They do it. Uh, they they have this method called list search, which is um, it takes in two parameters list search Sorry guys, my pencil is a little off. I have to sharpen it our list search search. Um, they have this they have L which is the List and k is the value you're searching. So this is the List and k is the value you're searching So what is the pseudocode? um, I'm going to create a pointer called, uh x Which is going to equal to start from the head Right, I'm going to start from the head. So this is going to be list dot head. This is just pseudocode guys in actual examples Uh, you actually have to code it in different languages. They they coded differently, but basically you would have a You would have a pointer that you create which is x equal to list dot head Then what you're going to do is you're going to check while it's not null and the key Or the value whatever Value is is not equal to k. Then we are going to just keep going to the next element and then after that we just return Okay, so what is the pseudocode? What is the pseudocode doing? Basically, the pseudocode is doing is is that sorry guys after we push this Okay, so what is the pseudocode doing? You're creating a variable x and you're going to point it to the Head which is the beginning of the list while x is not null, right? So that means it's not empty. Um, you and the key is not equal to the key that you're searching for You're going to keep going to next All right after that you're going to return x which is the pointer that you're at Right, so in this case if I'm at I'm at a right I'm at a and I keep going uh I'm going to create a variable x. I'm going to start at a right Which is head the list is uh, this is the list and I'm going to start at the head which is a I'm going to keep going next b and then c Until I until I reach the keys value, which is equal to the one I'm searching for it Like I'm trying to search for c is going to equal to c and then I'm done, okay So this pseudocode is basically doing is that we are checking for We're keep we're going to keep going until we reach the end Right the x not equal to null this x not equal to null basically is telling you that uh, you're going to keep going until you reach Then because uh, no at the end At the end of the list, it's going to point to null. So sorry guys. I forgot to tell you but uh, no means empty All right, no means that there's nothing there. So like at d's end, it's going to be like null stuff like that Yeah, so this what this is searching for in this search is that it's going to start from the head I'm going to keep going until we haven't reached the end of the list And uh, our key value is not equal to it, right? Then we're going to keep going to the next value Uh, after this condition Breaks right like this condition is done Then we're going to return the whatever value or key value that we are searching for right If we reached it, um If if it's not if it doesn't exist in the list at all Then this is actually going to just return null because this is going to keep going until we reach to the end Right. Uh, while x is not equal to null that, uh, remember the last value The last value you're pointing at is null and that this checks for the end of the list. So yeah, this is uh Searching through a list. All right, guys. So what if you want to insert an element before the head at the head or before the head? Okay, so when I said at the head that this should be before the head But let's say we're going to add insert an element before the head so In this case, if I'm going to insert an element before the head This is the pseudocode that clrs gave us and I'm going to explain that to you guys right now. So Let's say I want to insert Let's say I have this list abcd, right? This is a little English abcd and the head is a So what do I do? I'm going to create I want to insert the element k I want to insert the element k before the head of the a So what do I do? I'm going to point k's next to point to the head Of a And then all I have to do is now just set the head pointer. So I'm going to get rid of the head pointer Right now. I'm going to set the head pointer to now point to the new element k And then that would that'll basically they'll basically just add the element k before the head of a in our list And that will do what it does So in pseudocode, this is what you do X is the element that you're inserting That you want to insert before the head of the element of the our list l So l's the list x is the element you're trying to insert So what do you do? You set x x is next to point to the list's head. So what we did that's what we did here right If the list's head is null uh is not null This is setting the previous to point to x. Okay, so in this In this case the setting the previous to to point to x If your list is a doubly linked list, you would do this So if if in our in in this case if this was a doubly linked list, like they had previous pointers all over you would point A's previous to point to k That's that's what you would do because uh if the Of the head lists head is not null You're going to point the previous of its head to point back to the element that we're trying to add Which is x. That's what you would do here Um, but if you're not if your list is not doubly linked list, you could just ignore this condition completely But yeah And then after that you just reset your list's head pointer to now point to x Which is the element that you're adding. Okay And that's what this that's what this code is doing list's head Is not going to point to the one that we just added and then um if you if this were a doubly linked list You would also point k's previous to be null Right, uh previous would become null. Uh, this is this is just to show that like Uh, you you don't have any elements before k because of your head node. This is just like This is just like a precaution, you know setting your previous to null. Um, yeah because in double in doubly linked lists, you want to make sure that Uh, your head actually the previous value of your head actually has no element So that's what you would do here. You would set k's previous to point to null So, yeah, that's that's basically how you would insert before the head And yeah, then let's go over uh deleting deleting deleting a node All right guys, so i'm going to explain to you guys how to do delete now So in the delete in clrs, they're they're assuming that this is a doubly linked list But how about I explain it with a singly linked list first and then I'll explain clrs's code So This is a singly linked list. Let's say I want to delete c Well, first of all, I have to find c first So if I want to find c I have to traverse through it and find c. So I have to find c So I have to get this value to delete Right, I want to delete c. So I found c Now, what do I have to do? Well, I have to get the previous Previous pointer of c and point it to c's next d. So that would that would remove this c But the issue with that is that I can't just get this b value You know the previous value of c because I don't have a pointer to the previous value, right? It's not a doubly linked list. So I would have to traverse again and then find this b value the b value Which is the the node before the value that I have to delete So I'll have to get this so once I got the previous value that's before the node I wanted to delete I just said It's next so b's next is now going to point to whatever what c was pointing to so that was that's d So now b's next is now d and then that's it now. I just have to set c's Then the value I'm going to delete it's next to be null And that would just remove it right that will move the value of a singly linked list okay So that's how you remove in a single linked list. You have to traverse through it keep track. You have the the previous Previous node that's before the one that you have to delete. You have to find the one that you delete You have to get the node that's before it and then set its next point to point to the node after That the one that you're going to delete. So that would remove the node completely. Okay So now let's go over the clrs's delete Their deletion. So in their deletion, they're using a w linked list. So now because they're using a w linked list Let's see be the a So let's say I'm going to delete c Well now because it's a w linked list. I have a pointer to the previous value So I don't have to traverse again and try to find the node before it. So now What do I have to do? What does the first code say? Okay, so this if so x is the one that we're trying to delete. So just access to delete l's the list, right? So what does their clrs's code say? Okay, so if x dot previous is not equal to null So here's just checking if what we're deleting is not the first value of the List this is just checking. It's not the first value of the list, right? Because if it's the first value of the list, we could just set the head to the next so yeah So The value we're trying to delete x which is c is c. So this is one or two delete and this is basically x This is x right So previous is not null, right? So our x is previous. It's not null Right. Our x is previous c's previous is b and this is not null. So what do we do? We set x's previous is next pointer. So x's previous is next pointer. So b's next pointer is going to equal to x's next So what's x's next x's next is c's next and that's d. So this will set b's pointer to now point to d Okay, so that's what this does Um, and then then what is a check If x is next so c's next d if it's not equal to null. Yeah, it's not null Then we also have to update the previous pointer. So we're going to set x dot next so c's next is d these previous we got to set to x's previous So this d's previous is now going to equal to x's previous x's previous Or c's previous was b. So that would get do that And that basically removes it So now whenever you traverse through the list list list, you would have a b d and d's Previous it goes to b. So then c's never literally never hear c was never here. So the c's now deleted. Okay, so that's what this code does it checks the previous value Updates the previous values next to be the whatever's after the one that we're trying to delete and then updates the One that now the node afterwards it's previous to be the ones To the node that was before the one that we're trying to delete. So that changes these Pointers to be point to the right one All right, let's go to the case of this case where it's the l statement. So in this case, let's say where I'm trying to remove the forehead So let's say I'm trying to remove uh Uh, let's see if b's next It also yeah, let's say I'm trying to move a right, which is the head. So here That's what this else statement checks. So if um When I'm trying to move so uh, the one I'm trying to move is a so X is a right now. Okay. So if a's previous is not equal to null in this case It is equal to null, right? So what do we do in this scenario when I'm trying to remove a All you have to do is just set the head pointer to point to a's next. So now head is now going to point to B And then that's it pretty much because now whenever we Traverse through this list we start whenever we always start with the head and we traverse afterwards So it's almost as if a never existed a never existed, right? A head is now pointing to that so head is now pointing to Whatever value was after the one that we're trying to delete head and it never existed So yeah, that's how you do delete delete nodes in a single link list and a double link list So what are what is the caveat of link lists? The problem is that You have to traverse through every single time. So that's o of n Removing and deleting this is removing and deleting Okay So like um, if I try I have to go through like searching searching and deleting I have to go through it find whichever value I have to delete I also have to find the previous node in order to update its pointers. So that's why it's o of n Um, just like just changing the the pointers that just that's just constant. That's o of one, right? If I'm just like the constant of stuff like that That's just o of one, right So yeah, that's how you do that's how you remove and delete in a link list. That's basically all of the link lists um For your comments subscribe, I'll check you guys later. Peace