 What's up guys, my name is Michael and welcome to my YouTube channel today. We are going to go over link lists All right, so this is one of the series. I'm trying to redo. I'd like to really old series We're just literally reading a textbook and that's not how I like to make tutorials now. So Nowadays, I'm just going to try to implement the code with you guys and I'm going to explain Okay, so this video is gonna be everything you need to know about link lists So first a link list is a type of data structure. Okay, where each of The data is in something called nodes So if you look in this picture data all the data values see the a b cd They're inside these boxes called nodes, right? Now as you can see here in each node for each of these boxes, there is a pointer pointing to the next node So you see a the data of a there's a pointer next pointing to b and then B is next It's pointing to C and C's next is pointing to D Then these final node points to null Okay, so the last value that these point to is null. Okay This is a singly link lists Okay, you could also see that there is a pointer called head that points to the first node of our list So what are the what is the advantages of a link list? Okay, the First advantage of a link list is that if I want to remove something or insert something It is pretty easy because all I have to do is literally just add something and point it to the next value I just have to modify these pointers that points to the next value if I want to remove something I just have to find the previous node that I want to remove Point it's next to the to the node afterwards. Okay That's inserting and deleting right now. What is the problem of a link list? The problem is that I cannot access the nodes without starting from the top and Moving all the way down. So if I want to find the nodes see I can't access the node Of the other nodes. I have to start from a go to B And then finally get to see because the only way to go through a link list is to go through starting from the top of the head and Traversing by going through next. There's no way to actually get a certain value So if I want to go the value D I literally have to start from the top of a and Continuously go to the next value of B C and they go to D. So that's the problem about link lists So how would you implement this? All right, so now I'm gonna bring up the code and show you guys in OBS Okay, so how would you implement a link list? Okay? So first of all we need to have a class called node. This node is going to represent the boxes in the previous picture that we saw before Okay, now remember in the boxes There's two types remember the actual specific data and the pointer that's going to point it to the next value So here we have two variables that are public The first one is the data and this one in our sync. We're going to use integers Into types. We're not going to use like chars or like strings But you could literally put whatever you want in here if you really want to Now next we're going to have a pointer called node star next this pointer Next is going to point to the next node in our link list Okay, so now how would you traverse a link list and to print things out? This is what you would do. You would have a method called print list. Look at this method print list We have a node that we're starting from this is going to be node star and Okay, and what we're going to do is what we're going to say while it's not equal to null We are going to just print the data and then we're going to go on to the next node The reason why we're saying wilds n is not equal to null It's because remember in the last picture the last node is pointing to null So if we were to just continuously Traverse go to the next node until we reach null. That's how you would print all your values in your list So that's why we say while our starting node is not equal to null We're just going to print the node and go on to the next value Continuously do this until we reach the end where n is equal. Okay, so there are a few simple cases that we could think about Before you write insert So let's say I want to insert something to the front of our link list so in this case How you would do it is you would just take our new node Have its next pointer point to the front of the link list and then now then we just reset the our head variable to point to the new Head that we wanted to have so in this case we have a b c d in our link list, right? So what they did was they created a new node called e Have its next pointer point to a because a was the original front of the head Right, then what they did was they just now reset the Head variable to now to point to e so now in our new link list e is now inserted in the front of a So that's what? inserting to the front of Is like in link lists so the code of what it would look like is this They called this in this scenario. They called this as push But really it's actually add to insert to the front But yeah, so here they have two variables called head reference, which is just a point the Head node that was referenced to and they also have the new data that you want to add So this is kind of strange in that they didn't actually create the node yet but yeah, they just have the new data that's passed in as a Parameter in the input for for push so here what you would do is you would create your new node So here that's what they have here allocate node So this calls node star new node is equal to new node So this is going to create the new node then they put in the data that was passed in from our parameters In our for the push so this is going to do new node Get the data variable for a new node. It's going to equal to the new data. So that's what this is new data Then they're going to make the next of the new node as the head so this new node This new node dot next now. I'm going to point to the head of our original List so remember we had a bcd, right? This is this part The first two lines is basically creating e Right, then they're gonna set ease next to point to the first node, which is a so that's what this line is doing So the new nodes next is going to point to the head Then they're going to move the head to point to the new node. So that's what this does Star head reference is gonna point to the new node. So that's what this method push does This push method is actually just adding inserting to the front of the link list Yeah, I don't know why they called it push, but it is what it is Okay, so the next method that we have is called insert after and what this does is that it's gives Given the previous node that we have want we insert a new data that's passed in right after that node So remember in our example a bcd, right this in order to call this method what this is Expecting is that you already found the node C. So that's I want to insert. I don't know f after C what this method is expecting is that I'm going to find the node C and I'm going to given that already found the node C. It's going to insert f after C Right, so that's what this function is doing So I know I said like in our example is a bcd But uh, yeah, but here it's like We used integers, right? But it could be integers or characters or anything you want For the data type data could be anything But yeah in our example here for the sake of this video. It's integers Okay, so in this method It is basically taking the previous node and inserting a new node after it so here we have This first part is just checking if it's null if it's not in the list can't the previous node can't be null, right? So this is just like a check If in case of something something went wrong before Right, so this is finding the previous node first. So So what this does is it creates a new node called new node goes a new node Sets the new nodes data is equal to the data that's passed in It's going to make a new nodes next point to the previous nodes next right so if we had like a bcd, right and We want to insert f after C What this does is it's going to create the node f, right and then It sets the data to be the node f right and then here what it's going to do It's going to make the next of the new node as the next of the previous node So in this case, it's going to create F F's next All right, so we remember we want to insert f after C. So what's it? This is doing? It's it going to set f's next. It's going to point to whatever was previous is next so Remember we want a bcd right and we want to insert f after C. This is basically going to get D It's going to insert f's next to be the next node after which is D So now f's next is going to point to D And then we're going to just move the previous nodes equal to new node the previous nodes next again equal to new node So it's going to set C Point to f so then in the end we would have CFD Right, so basically I'll just show you guys what I mean So remember we have a b C D right this was what we had before let's say our list is like that And I want to insert f. I want to insert this new node f after C So what this method is doing is that it's basically saying hey, I got this I'm going to create this node called f and I'm going to set it between C and D Right, so to do that what I'm going to do is I'm going to get D So I need to get C's next which is D and have this f point to D right So then f is going to point to D Right, and then what it's going to do it's going to take the pointer of C's next point to f So we're going to get rid of this D and point C's next It's going to point to f So then in the end we have a b c f e Okay, so that basically means that we find actually inserted f between C and D. So that's what this method does So yeah, that's what this does. So previous nodes next. That's just taking C's next point to D So that's what it does C's next Points to f. Okay, so the last function that we have is a pen which is just going to insert something to the end I don't know why it's called a pen, but it's just insert something to the end of the link list so let's say I have like example was a a b C D and I want to insert E after So I'm going to insert E into this link list what it's going to do is it's going to Get the node D and it's going to create a new node Called E and it's going to insert E after D So that's what this says. So this is just adds The new node E to the end of the link list So how does this function work? So first of all you're given the head and you're given the data that the new data you want to add so what this does is it creates a new node calls a new node and then What this is doing yeah creates a new node and then this part is going to add The data to the new node. So that's what this does new node data is going to equal to new node a new data Here what this is used at doing is it's going to find the last node and To do that You're gonna have to Yeah, okay, so you're gonna have to continually traversing through the link list until you reach the end So remember that's what I said in the beginning of the video. So you have to literally do that So that's why they need this node last So no last points to head and they have to start from the beginning So that's why they say node last equals to head. So this thing is just pointing this node to the beginning They have this variable called last they're just pointing it's a beginning. They have to keep going to the end later on So they're gonna use this later Yeah, oh, okay here remember our new node. It's next has to point to no the reason why is because It's last value is no because it's not the end of the list, right? So we're adding We're adding e to the end of the list a bcd You so if I go back to this example a bcd e right ease next has to point to no Because that means that we're at the end of the list, right? We're at that last value Otherwise, this is not a otherwise. It's not the last value. So yeah, that's what it has here. So here New nodes data equal new data new nodes next is going to point to no so this that would mark it as the last value Yeah, so that's what this does new nodes x points no, okay, so Here's just an exact case in case if the list is already empty If the list is already if less is empty already, then we just have to change the heads node to the new node Right because we don't have to Like the list is empty. So we just have to create we had we just have to change their head Pointer point to the new node. We don't have to like loop through add it to the end Because there's nothing to loop through for Okay, so what this does is while last next is not equal to no It's just this is just gonna keep going to the next until we reach our last node. So Yeah, while last next is not equal to no, we're gonna keep going until we reach the last node Then once we reach the last node, we're going to set it equal to other than you know that we created so that's gonna create This this is what it's doing. It's basically going to So in the beginning we have ABCD, right? And let's say I want to create E So I'm gonna create a new node called E And a point it's next to null and then I'm gonna find the last node. So to find the last node I'm gonna start from a go to B You go to C and go to D until I finally reach the last node where these next is not equal to null So then I found the node D and then we're gonna set the next pointer These next is going to point to a new node that we added and that's going to be E So we're gonna do this that'll be E. So then in the end we have ABCD E and we inserted E Right after D like we wanted to before So, yeah, this is that's the case if you want to insert E after their original list of ABCD So that's this method okay, so now we're gonna have a driver code that actually runs our code and Actually creates a list because currently we just have a bunch of methods that creates linkless and it doesn't actually You have to run it, right? So here we have the start starting node in our main method The first we create the head and just point it to null. So then this is gonna create an empty list So now now we're going to just insert to the front. So we're gonna insert six to the front This is the method append. This is basically just inserting it to the front I don't know why they called this append in the implementation Which should be insert to the front because I think insert to the front is much easier to Understand but anyway, they called it a pen, but it's just inserting six of the front so in the end here you're gonna have six pointing to null, right because Six is next is gonna have B pointing to null because there's nothing after it. It's right because we only just inserted six Then we're gonna push seven to the beginning so this is gonna insert seven to the front So here we're gonna have seven points to six We're just points to null because we just append it's seven to the front. So we have seven six null And then let's try and then here we're gonna push one to the front. So here we're gonna have one seven six point to null Then we're gonna insert we're gonna add to the end append to the end So then what's gonna happen is we're gonna append four to the end. So this becomes one seven six four and goes to null and then we're going to Insert eight after seven so to do that We just so if we look at our initial list one seven six four, right? The head is currently pointing to one and if I we if I want to insert eight after seven What is seven so seven this the sevens note is the heads next so that's why they pass in Heads next and as this variable of the previous note If you wanted to insert after six you literally have to find the node six first by keep going next Until you find six and then you could insert Insert it so here we have insert after heads next which is seven Right they pass on the previous node for that and then they're gonna insert eight So it's gonna insert eight after seven so we're gonna have one seven eight six four No, and then it's gonna print the list. So then we're gonna have one seven eight six four No, and if you run it it should ideally print out the list correctly Okay, there's some errors. Let's see what's the problem Let's see see what's the problem Let's take insert after already has a body I copied this twice Oh, I already I copied this twice my bad. Let's just get rid of this. No, it should work Yeah, so we have one seven eight six four if you wanted to have like a bcd You would have to change it to characters and pass in characters You would have to change it to characters and pass in characters. So it's about the same thing Yeah, so now we've won seven eight six four So, yeah, that's the gist of inserting of link of a linkless implementation Now let's look at deletion. So I'm gonna talk about how to delete a node from a linkless So if you look at this example picture if I want to delete the value see how would I do it? Basically, I would have to find the node before see so be and I need to set this pointer to Point to whatever is after see so whatever's after see is D So I'm gonna set bees next pointer is gonna equal to D and then I'm gonna free see So that means when you say free and C plus was it just delete C So that's how you would delete it because in the end after when we traverse through the values It would be a B and then D Okay, so now let's go over the code of delete node I'm not gonna go over how to run it because basically you just delete it right you just call it delete node But um, yeah, we're gonna go over this sample code of how delete node works okay, so This first case is just checking if I'm if the value if the node I want to delete is the first node So if it is the first node Which is what this is checking right? It's not null and the In the temper, so it's not null and it's equal to the key Then all you have to do is just go to the next node and then delete the first node. Okay, so This temporary variable is just pointing to the first node and then this is just checking if you're deleting the first node So if this is this if you're actually deleting the first node All you have to do is just set it equal to the next Next node and then just delete the previous node Delete the one that you were at before then return So, yeah, that's what this temporary variable is this temporary variable Just is just pointing to the first node currently to the head. Okay. All right now now To actually delete the node you have to search through to find the previous node delete The same thing with like adding you have to search through it That's what's to paint about it. No link list. You have to actually find it Okay, so to search through it This is what this is what the code does So we have a temporary variable and it's pointing to the head Had to know remember Right here. It's pointing to the head Reference. So here what this is doing is it's going to check. Hey, is my next node Is is the current node? I'm currently at is it equal? Is it is it the value that I want to delete? That's what's checking Right, if the temporary node if it is it the value I want to delete. Okay So if it's not equal to key then I'm just gonna So I'm just going to the next node so temporary equal to the next and I'm going to store this previous variable is going to store the node before it So I have a previous variable called brief You just say originally we said to know but while we're traversing it. We're keeping track of our previous node Because then in the end we have the previous no, then we could delete it So that's what this while it was doing All right So if the if the node that we're trying to find it is in all so it's not in the list We just returned because we can't do anything. That's why we have this That's where we have this f statement All right. Now once we found the previous node that we want to delete we just set the previous node The nodes next equals to the the node that I'm currently at it's next so that so this is This is what it's doing. It's Let's say I have a B C D right So what this is doing is going to find let's say I want to delete C This code is going to find B So let's say we found B here and it's going to point B's next. Yeah, hold up It's going to point B's next to point to What's ever after C so whatever after C is D. So it's going to set B point to D and Then yeah, then it then that's what it does so here previous dot next is a Bees next so it's saying bees next it's going to point to Temp dot next and temp temp is our current value want to leave which is C So temps next is gonna times next it's D So it's going to set these next to point to D. So it would remove C And then here it deletes C by freeing it up the memory Yeah, in Java, you don't have to do it, but in C plus plus you have to do it So yeah, that frees up the memory And yeah, that's basically how you delete a node There's also another way to do it recursively I'm not I could go over how to do it Yeah, I'll go over how to do it so Yeah, I'm gonna copy it and I'll go over how to do it, but I'm not gonna run it though for this part So if you want to do it recursively, it's the same thing. You just have to find it and then actually delete it Okay, um mercy. Oh, why is it like that? That is not right. Okay? This is definitely not right, but whatever. Um, why is it like that? Okay. All right. Um, I Don't know why it's doing oh Why is it passing their reference of the pointer node? Super weird. Okay. All right. So if you want to do it You recursively Basically, you just have to keep calling next and then once you find it you just delete it Ideally you don't you don't Normally, you don't really do it this way, but Okay, uh, it's still each week. It's only this if statement expected declaration Okay, ah, whatever I think it's like that. Okay, expect the declaration to free. Okay. Yeah, it doesn't matter. Um, okay So here's how you would do it They should We should actually make this by how I wrote this twice. That's weird Okay Let me just get rid of this So I'll go over how to do it recursively. It's giving you some weird Okay, it's giving me compilers, but it's fine. We could just ignore it for now. I copy the incorrectly Okay, so if you want to delete it recursively It's actually they did some weird things. Okay, let's get rid of that. Okay. All right. Okay So if you want to delete it recursively Basically you have to keep going next recursively call The function and keep going next until you find the node and once you find the node you have to free it Okay, so here's how it works. Um, if the head is null that means We reach the end of the list and that we can't actually delete anything So that means that the element is not actually in the list. So for that we just like Just print out it's not in the list and we delete it, right? We should we return it because we did we can't delete it All right Then what we do is we need to check if we're actually at the node that we want to delete so for that we just check the value We check if the heads and data that we're at is equal to the value So value is a data that's we passed in to this method of delete node and It's the value that you want to check The value that you actually want to delete. So if we're actually if the data that if the Four we're at in the head is equal to the value that one delete Then we're gonna set a temporary Pointer of node. It's gonna equal to the current node. We're out of head We're gonna set head to equal heads next Okay, and then we're just gonna free up the Free up the know that we're at and then return Otherwise if we're not at the current node, we just recursively call next and pass in value until we reach the end of the list So, yeah, that's basically how you would delete it recursively. I didn't copy it correctly. So that's why I have a weird Thing here, but yeah That's basically how you would delete something recursively. I hope you guys enjoy this video This is everything you need to know about link lists at least single link lists the rest of the Functions you might need to implement They might ask an interview or just variations of Inserting and deleting which is something that which is not that difficult once you understand how to insert and delete So, yeah, we guys wrote this video right come subscribe. I'll check you guys later. Peace