 What's up guys Michael in here welcome to my YouTube channel today We're going to go over how to delete a duplicate value nodes from a sorted link list So basically if you have one two two three three three three three No, it's going to return you one two three because it removes all the duplicate values and this link list is already sorted So that's a good part about it. So It removes all duplicate values. So you don't have like just no duplicates like there's no Duplicate of values. It's just one two and three See, yeah, that's just a gist of the problem statement. There's nothing else to talk about. So I'll just go into the code So I Did this recursively You could do this like iteratively if you want, but I did it recursively. So basically if you think about it If the the base case is like if the list is null Or the next pointer is null then I just return the list and the reason why is because this is going to Let's say we had Let's say we had a this list this pointless. Okay, this list of one two Two and then three, right? So let's say And this goes in all So let's say you're at Let's say you're at Null here. You're at the end of the list, right? Well, that means you're you're pretty much done. You're done here So you're gonna return The original head right the list of your current list, which is just your current this should be called head I don't know why they call it LL list. It's really stupid But anyway, this is like if you're at the end point you just return Itself if the next pointer is null also, then you just return itself the reason why is because like let's say you're at this point Like almost close to the end. You do you want to return three here, right? You're you don't want to return like You don't return null Because you're at the last of the end of the node. It's just by itself. You just want to return this Okay, so that's the base case all right, otherwise What we're gonna do is that what we need to do is we need to check if the current data that you're on if that's equal to the next data so like if if this two since we're trying to remove all Duplicates, so duplicate values of twos. So as you can see here, this two these two twos are duplicates, right? So we need to check if let's say I'm on this current node here of two I have to check if this two is equal to this two, right? to remove it because we know that Sorted values are like Because we know this link list is sorted from beginning to end We know that if we sorted since it's sorted, we know The duplicate values are going to be one after the other, right? That's the reason why we do this All right, so we have to check if the current node is equal to the next node Okay, so if it is equal to the next node, what I do is I just return the List after it. So I recursively call my current Function and I pass in the next value. So what this means is that I'm going to just recursively call The next value of this, right? So I'm gonna recursively call this next value and I just return This next value Yeah then The reason why I do this is yeah, the reason why I do this is Because if you recursively just return the next Value of the recursive call it's going to set your two later to equal to the next value of Three later on so here and the next part of the code is basically what I'm doing Otherwise, if it's not equal to it, so let's say I'm at let's say I'm at this point So I'm recursively calling this function on the next value And I have this right I have this two and I have this three right I'm going to set the current list of this two is next to equal to the removed duplicates of the next value So what this is doing is that it's going to set the This current two is Going to set it equal to Yeah, I'll set this two is going to equal to the next value of two So it's going to set twos is going to equal to point to three and then it just returns the current list of currently at two So then at this point this Both of these twos are gone and we just have one two and three So I could show you guys what I mean how this works in the Method signatures, so if I'll show you through the recursive calls because it's pretty hard to under Think about this if we're just doing it by itself. So here. Let's just Okay, so here is let's say we're at so here. Let me just paste my link list single like this Yep Okay, we paste these we paste this Is this Never paste this All right, and then we have this single like this note. This is a class Ideally this should work. Yeah. Oh that's that this has to be public. Oh, let's make it struck Then I'll have all the public methods Yeah, okay, so let's actually Create our link lists of like three different nodes. So let's do that And I'll show you guys what how this code works underneath the hood Okay, so let's create our link list. Let's say I have one two two three so I'm gonna create like this here Okay, so then we have this List new single link list, you know what I'm gonna create a I'm gonna create a constructor because this is really like this is really annoying. Um, yeah I'm gonna do this real quick. Let's create like three and then One and the next point to like null. No, why can't I do this? Okay, um Okay, so let's actually create like one two two three so I'm gonna copy this paste It's gonna point to two and then this is gonna be three One two two and then one more three. Okay, so we have like Or one and two and then two Let's call it like one That's three. Let's call three two two two three two two two And I'll call this one. Okay, so then One's next gonna be two one and then two two's actually gonna be here Three is gonna be here and three is actually no point. Okay. So now once we have our all our lists Let's actually Move duplicates and then we print it out later. So let's call it redo move duplicates Passing in one and then I'll just settle it one is equal to this. Okay All right, I'll show you guys how this works. So we have one two two three And we're gonna recursively call it We're gonna call remove duplicates. So here was what it what is it gonna do? we're gonna go again inside here and First of all, this is not gonna do anything right because they're not know Does one equal to two right? Does this one equal to this two? No, it doesn't currently it doesn't right? Let me actually just Get rid of this Yeah, currently it doesn't so we're just going to this part. So it's gonna recursively call lists next is gonna Recursively call on the next function. So it's going to recursively call on this. Okay So this one's next is going to point to recursively call here now does two's next equal to the next node of two Right. Yeah, it does Does this to does this to equal to two? It does right So what it's gonna do is it's going to call return remove duplicates on the next one So what is this going to do? Now our data is just gonna be two and then three So it were what it did was it's going to Go back here. They're recursively called on this next one on these right two three no, okay And then does it does two equal to three? Yeah, there's this current node of two equal to three. It does not So it skips that then it says to our current twos next is going to point to move duplicates of Recursively call on the next one. So currently we are lists Two is two and we're gonna say it's next is going to recursively call the next one. So Who's next is going to recursively call on the next one of three So if we're cursively calling on three, we'll be like that, right? We're gonna recursively call on three. So Yeah, I'm gonna go inside here now. It is three equal to no pointer or three is next to pull it does so we return three so After this occurs twos next is going to point to three Okay, so two we have a data lesson to its next is going to point to two Three as you can see here, right twos next is three So what this did was it set twos next is going to point to three now Yeah, twos next is pointing to three now we're gonna return to so once we return to it's gonna return to here and we go back up to the top and We return to three here Okay, and we're not setting to the the previous nodes equal to remove duplicates because they are equal to So now it just comes back to the top and we have two three here Now when we come go back to the next recursive call Our list is now one So our current head of our list is this one and this one's next is going to point to remove duplicates value of two and three See now we have one two and the next is three and then it's all and then we return one here So yeah, that's pretty much the gist and that's why we have one two and Three basically When you do a recursive call and you're removing duplicates what we're doing is a recursively calling on the same Same method and then if it is equal to it We just skip this node and go to the next one And we don't actually set our pointer to equal to the next node. We just skip it like we skip This current no, we don't set it point the next one to equals the next one. So that's why we just return our following node lists next So, yeah It's the gist of this code. I hope you guys understand what I was doing ray com subscribe. I'll check you guys later. Peace