Welcome back.
Let's now go ahead and design the singly linked list.
And let's also implement the instance methods as we discussed in the previous video.
Now for this let's first write a class node.
Now we will use this class to create each node which is going to be each element in the singly linked
list.
Now over here we'll need a constructor.
And let's pass the value over here to the constructor.
And then we will just say this dot value.
Is equal to the value which is passed over here to it.
And then let's say this dot next.
Is equal to null.
All right.
Now we also need the singly linked list class.
So let's say class singly linked list.
And we have a constructor over here as well.
And nothing is passed to it.
Now let's just say over here this dot head is equal to null.
And we can say this dot tail is equal to null.
Now we can also track the size of the linked list over here.
So let's do that.
So we can say this dot size is equal to zero at this point.
All right.
Now over here we will go ahead and write the instance method.
So we'll have a get function.
So get index.
That is one of the instance methods asked in the question.
Then we have an instance method to add at the head.
So we can write over here add at head.
So these are just placeholders.
And over here the value has to be passed to it.
All right.
Now let's go ahead and write the remaining instance methods.
And let's just have placeholders for them.
So we have another method to add at the tail.
And the value is passed to this method as well.
And then we have another function to add at a particular index.
So add at index.
And the value is passed to it, and the index also is passed.
So let's say index comma value.
All right.
And then finally we have a method to delete at a particular index.
So delete at index.
And in this case only the index will be passed to this method.
All right.
So these are the instance methods that we will write.
And then to initialize an object we can just say let's say let's just call that new object.
Let's call it um SL.
So we can say const SL is equal to new.
And then we can just write singly linked list.
So we are using the class to create a new object of singly linked list.
So that's the first part of the question.
So that is done over here.
Now let's complete this part.
And then we will just test our code.
So we will go one by one.
So let's get started with get index.
So let's say we have a singly linked list and we are calling the Getindex instance method.
And let's say the index is passed.
Let's say the index is 1 or 2, etc..
Now we have to keep traversing through the singly linked list till we reach the node where the index
is equal to the index passed over here.
And again we don't index a singly linked list like an array.
So we cannot access the particular index in constant time.
So let's go ahead and implement the function as we discussed in the previous video.
Now to start with let's just check if the index is valid.
So if the index which is passed over here is less than zero, or if the index is greater than the size
and we are tracking the size over here.
So that's coming handy for us.
So if the index is greater than the size.
So we can say if index greater than or equal to the size right.
Because the last index is going to be the size minus one.
Because in the question it's mentioned that the singly linked list is zero indexed.
So the index of the first node is going to be zero.
So if index is greater than equal to this dot size.
Right?
So if either of these is true, then we know that the index is invalid and we can just return minus
one.
So that's mentioned in the question.
So if the index is invalid we return minus one.
Now if this is not the case we proceed.
So we'll have a counter.
So let's say let counter or let's say counter is equal to zero.
And then let's say we have another variable let's call it current.
Now this is going to point at the head at the moment.
So this dot head so current is equal to this dot head.
And then we will have a while loop.
So we will use this while loop key to keep traversing the singly linked list.
As long as the counter is not equal to the index.
All right.
So if the counter is not equal to the index inside the while loop, what we will do is we will just
move ahead.
So current is equal to current dot next.
And then we will just increment the counter so we can say counter plus plus.
All right.
So when we are out of the while loop, the current over here is going to point at the node where the
index is equal to the counter.
So we can just return that particular node.
So we can say return current.
And that's it.
So this is our Get function.
Now let's go ahead and complete the remaining functions also over here.
So the next one is to add at the head.
Now if we are calling the function add at the head and the value is passed.
So what we need to do is we need to make a node with this value.
So let's do that first.
So for this we will make use of the node class over here.
So we can say const node.
Is equal to new node and then we pass the value to it.
So this will call the constructor over here.
And it will create a node.
And the value of that particular node will be the value which we just passed to it.
And this dot next is going to point to null at the moment.
Now we just need to make this point to the head right now.
Before that let's check whether the singly linked list at this point is null or not.
So if not this dot head, that means if the head is null and the singly linked list is just an empty
singly linked list and the head itself is null.
If that is the case, then we have to make this particular node which we created the head.
So we can say this dot head is equal to node, and the tail also is going to be the node itself, because
there is just one node at the time.
Right.
So we can say this dot tail is equal to node.
And then later once we are out of this we will increment the size.
So we can say over here this dot size.
Plus, plus.
Now, why am I incrementing the size over here outside?
Because we'll have an else condition also over here right now.
In either case, we'll have to increment the size.
So it's common to the if and else part.
So that's why I've done it over here.
Now again if if if the head is not null then what do we do.
So we, we have seen that we have created a node.
Now we need to make this point to the current head.
So we can say in the else part node dot next.
Is equal to this dot head.
And then we need to make the new node the head so we can say this dot head.
Is equal to node.
All right.
So that's it.
So we have added at the head and let's say after this we're just going to return the complete singly
linked list.
So for this we can say return this.
All right.
So that's the end of this function.
Now let's go ahead and write the next one which is to add at the tail.
Now again the value is passed to us.
So we're going to create a node out with this particular value.
And for this we will make use of the node class which we have written over here.
So let's use that.
So over here we can say const node is equal to new node with the value which is passed to us.
So after doing this we are going to check whether the singly linked list is null.
So let's check if not this dot head.
That means if head is null, that means there is nothing in this list at the moment.
If this is the case, then we have to make this particular node which we just now created the head.
So this dot head is going to be equal to node.
And the tail also is going to be equal to this particular node.
So we can say this dot tail is equal to node.
All right.
And if that is not the case if that is not the case then what do we do.
We have to make the current tail point to this node because we are inserting at the tail.
Right.
So we can say this dot tail dot next is equal to node.
And then we can say this dot tail is equal to node which is updating the tail to the node.
So we have we have made the previous tail point to the node.
And then we are updating the tail to this node over here.
So that's what we're doing over here.
And then because we have added a new node let's increment the size.
So we can say this dot size plus plus.
And that's it.
So let's say after adding at the tail, we will just return the complete singly linked list.
So over here we can say return this.
All right.
So that is it.
Now we are done with this function.
Now let's continue and complete these two.
So the next one is to add at a particular index.
Now if we want to add at an index it's going to be similar to what we've done over here.
Now first we are going to check whether the index is valid.
Now again in these two cases we did not have to check that because it was directly mentioned.
The the function itself is to add at the head and add at the tail.
Right now over here we could add at the head and tail using this function also.
But then we will have to mention the relevant index.
So let's get started with this.
Now as we have checked in the get function.
First let's check whether the index is valid or not.
So let's use the same line of code over here.
So first we are going to check if index is less than zero or index is greater than equal to this dot
size.
Again over.
Previously we were checking for greater than or equal to this dot size.
But because we are adding at the index, let's only check greater than this dot size.
So if it is equal to this dot size we will just make the new node the tail.
All right.
So we will make it the tail.
And we will make the existing tail point to the new node.
So over here we are just checking if index is greater than this dot size.
Then we will say it's invalid.
Now in the in that case let's just return a message.
We can say invalid index.
All right.
Now, if the index is not invalid, let's continue.
So first let's check if index is equal to this dot size.
So if index is equal to this dot size then we just need to insert at the tail right.
So we just need to insert at the tail.
Therefore we already have this function.
So I'm just writing this code in a manner that we can reuse whatever we have written.
So we already have the added tail function over here.
So we can just return.
This dot add a tail.
So let's do that.
So we can over here return this dot.
Add a tail.
And we are passing the value to it.
All right.
So that's it.
So if index is equal to this dot size we're just adding at the tail.
Now if that is not the case there is a possibility that index may be equal to zero.
Right now.
In this case we're just going to add at the head right.
So again we can just return this dot add at head.
And we're going to pass the value to it.
All right.
Now again if it's none of these if none of these is true then we continue.
So in that case let's just create a node with this value.
So we can say const node is equal to new node with this particular value which is passed to it.
And then we have to find the previous index right.
So again what is it that we're trying to do.
Let's say the linked list looks something like this.
We have a node with value one pointing to a node with value two pointing to a node with value three.
And this is let's say pointing to null.
Let's say this is how the linked list looks like.
And let's say we want to insert at at position one or index one.
So at index one we have two right.
So let's say the value over here is five.
So the output which we want is one pointing to five pointing to two.
And then that points to three and that points to null.
So this is what we want.
If you want to insert five at index one.
So what is it that we're doing.
We have to find the previous node.
So this is the node at the current index which is one.
We have to find the previous one.
And we have we have to make the previous point to the new node which we have just created.
And the new node which we created has to point to the existing node at the index.
So this is what we're going to do over here.
All right.
So first let's find the previous node for this.
Let's have a variable.
And we can just call it prev.
And prev is going to be equal to this dot.
Get at index minus one.
Again we are making use of the get function which we have written over here.
So this will return the node at the index.
And over here the index which is passed to the function is index minus one.
So this will give us the previous node.
All right.
Now once we have the previous node let's also have another variable.
Let's just call it temp.
Now this is going to be equal to prevnext.
All right.
Which in this case is going to be this one itself.
Right.
So the the node, the node at the index itself is going to be temp.
Now what we have to do is we have just we have we just have to make these connections.
That is prev dot next.
Has to be equal to the node.
All right.
And then we have to say node.next is equal to temp.
So you can see we just made these connections.
Prev down.
Next is equal to node.
Is this connection from 1 to 5.
And then node dot.
Next is equal to temp is the connection from 5 to 2.
So we just made these connections and that's it.
So we have inserted the node successfully.
And now let's just increment the size.
So we can say this dot size plus plus.
And after inserting the node let's just return the complete linked list.
So we can say return this.
All right.
So that's the added index function.
And finally let's go ahead and write the delete at index function.
Now the interesting thing over here is we made use when we wrote the added index function.
We have made use of the functions which you have previously written, which is added tail and added
head.
Right now in this case we don't have those functions.
So we'll have to take care of all the possibilities in the function over here itself.
So let's go ahead and do that now.
First we are going to check if the index is valid.
All right.
So if the valid if the index has to be valid then it has to be not in not less than zero.
And it should not be greater than equal to this dot size.
So let's use the same code over here.
So if index less than zero or index greater than equal to this dot size then we will just return invalid
index.
All right.
So it can be just a message.
Invalid index.
Now again notice over here it's greater than equal to this dot size because it is zero index.
So if there are let's say this if you are looking at this singly linked list over here you can see the
index of the last node over here is going to be equal to two.
And the size is equal to three.
So if if you are saying delete index at position at index three then there is nothing over here.
So that's why we have greater than equal to over here.
All right.
So if the index is not invalid then we proceed.
Then let's just check if the index is equal to zero.
Which, in which case we'll have to delete from the head.
Right.
So let's just have placeholders.
And then the other possibility is if index is equal to this dot size minus one.
Which means that we have to delete the tail.
Right?
So delete the tail.
And then there is another possibility which is the generic case, right?
Which is something else.
Delete another node.
All right.
So these are the three possibilities.
So let's go ahead and fill these parts.
So first if you are going to delete at the head.
So what do we do.
We'll need a temporary variable.
Let's say over here we are going to return the node that we are deleting.
All right.
So let me just write a comment over here.
We will return the node that we are deleting.
Again, you can ask your interviewer if this is fine and if he or she is fine with that.
So let's now implement it in this manner.
And if you want the complete linked list after deleting, you can just return this as we did in the
case of addition.
Now let's proceed.
Let's say we are deleting the head.
So because we want to return the node that we are deleting.
So let's store it.
So let's say let temp is equal to this dot head.
All right.
And then we are going to say this dot head is equal to head dot next temp dot next.
So what does this mean?
We are making the next node.
Let's say we have this linked list over here.
So temp is going to be node one.
And then temp dot next is two.
So we are making two the head.
So we're saying this dot head is equal to the next node.
So that is what we're doing over here.
All right.
And then we will just decrement the size.
So this dot size minus minus.
All right.
And then finally we also have to check if this was the only node present in this list.
So after removing the head let's check whether the size became zero.
So if this dot size is equal to zero in that case we have to change the tail also to null right.
So this dot tail.
Is equal to null because we are tracking the head and the tail.
So that's why we have to do this.
If you don't do this, then the tail is not getting updated right.
And we have removed every node.
So the tail has to be updated only if you are deleting from the head where the singly linked list just
has one node, in which case the head and the tail is the same node itself.
Now that's it.
So after this we can just return the node which we deleted, which is temp, which is stored in temp
over here.
Right.
Because we have let temp is equal to this dot head.
That's it.
So that is deleting from the head.
Now let's proceed and take a look at deleting from the tail.
Now again for this let's store the old tail.
So let's say let old tail is equal to this dot tail.
And then we are going to have a new tail, right?
So we have to find the new tail.
So the new tail is going to be equal to this dot.
Get at index minus one.
Right.
Why is that so?
So in this case the index is equal to the size minus one.
So let's take an example.
Let's say again we have this example over here.
So let's just copy it and get it over here.
Now let's say we are delete.
And the index over here is equal to two.
So the size is three and three minus one is two.
So let's say index is equal to two.
So we want to delete this node.
So we are going to find the previous node right which is index minus one.
So two minus one is equal to one.
So when we get the node at index one that's going to be the previous node which is this one over here.
All right.
So this is going to be the previous node.
And now because we are removing the tail this is the previous node is going to be the new tail.
So we have just found the previous node.
And let's just call it new tail.
And then we have to set the tail of the linked list to the new tail.
So we can say this dot tail is equal to new tail.
And then we also need this to point to null.
Now currently this node over here is pointing to three.
But because we are removing three we need to make two point to null.
So that is what we are doing over here.
So we need two to point to null.
All right.
So let's do that over here.
We can say new tail dot next.
Is equal to null.
All right.
So that's it.
And now because we have removed a node let's decrement the size.
So this dot size minus minus.
Now over here, we don't need to check whether size is equal to zero.
Let me just write a comment over here.
Don't need to check if the size is equal to zero.
Because in that case it would already be taken care in the previous right.
So don't need to check if size.
Is equal to zero because this would already be taken care over here.
Right?
So if the index is equal to this dot size minus one, and there is only one node in the index in the
singly linked list, then the index would be zero itself, right.
So we are already taking care of that possibility over here.
So we won't come here itself if there is just one node.
So that's why we don't need to check whether the size became zero after we decrement the size of the
linked list, and then we will just return the old tail, which is the node that we have deleted.
So we can say return old tail.
All right, so that is it.
So we have, uh, taken care of the function where we are deleting at the tail.
And then finally we have the condition of deleting another node.
So let's continue with that.
Let's again take a look at this example for this.
So let's say this is the singly linked list.
Now let's say we want to let's say we have three also over here.
All right.
And let's say we want to delete this node over here which is two.
Now in this case we have to find the previous node.
And we need to make the previous node point to the next node of this particular node.
That is what we will do over here now.
So let's go ahead and find the previous node.
So we can say let prev is equal to this dot.
Get at index minus one.
So this will give us the previous node.
And then let's also store the node that we want to delete.
So let deleted node.
Is equal to prevnext.
All right.
So that's the node that we want to delete.
So prev is equal to one.
And prev dot next will give us two which is the node at index one.
And let's say we want to delete that particular node.
So then we just go ahead and say prevnext.
Is equal to deleted node.
Dot next.
Right.
So we are making this connection from 1 to 3.
So that is this line of code.
And then we will just decrement the size because we have deleted a particular node.
So we can say this dot size minus minus.
And we can return the node that we have deleted.
So return deleted node.
And that's it.
So this should work.
Now we're done with writing all the instance functions.
Instance methods.
So let's go ahead and test our code.
Let's say we want to use our code and first create a singly linked list that looks like this.
Let me just draw this over here so that we can visualize it.
So let's say we want one which points to two and which points to three.
And this points to null.
So let's go ahead and create this singly linked list using our code.
So first let's add at the head.
So let's use the add at head um for instance methods to insert this one over here.
So let's we have already created an object SL so we can just say SL dot add at head.
And let's pass in the value as one.
All right.
So let's go ahead and run the code.
All right.
So you can see we are getting back a singly linked list.
And it has one node.
So that's working.
So let's take a look at it.
So the head is having the value one.
The node with value one the size is one.
And the tail also is the same node.
So yes this is working.
So let's continue building this singly linked list over here.
Now let's test the added tail.
So let's say we want to add uh this two over here at the tail.
So let's go ahead and do that.
So we can have SL dot add at tail.
And the value is two.
All right.
So let's take a look.
So we have the singly linked list.
The size is two.
And you can see the head is one and the tail is two.
So we have built these two nodes.
So it's working.
One is pointing to two and then two is pointing to null.
So yes that's working.
Now let's again add three over here.
So let's use the add at position.
So let's take a look at our added position over here.
Now we are checking if index is greater than this dot size only for returning an invalid index.
So let's just check that first.
So let's say we say SL dot add at index and we pass the value 012.
Let's say we pass the value three.
All right.
So the size currently is two.
We are having an index which is greater than two.
And let's add it.
So over here you can see we are getting invalid index.
So this is not valid.
But if we have this as two right.
The index is two.
And let's say the value is equal to three.
And that's the correct order right.
So let's just check that.
So in our added index index is the first parameter and value is the second parameter.
So let's add at index two the value three.
And now you can see we have a linked list with the size equal to three.
The head node is a node with value one, which is correct.
The tail has value three.
One is pointing to two, which is pointing to three, which is pointing to null.
So yes this is working.
All right.
So we have tested the added head, added tail and added index function.
Now let's try to get uh now let's try to get the node at a particular index using the get method that
we have written.
So this is the singly linked list.
Now let's use the get function.
So over here itself we can try that.
So let's go ahead.
So we have the singly linked list over here.
Let's say SL dot get.
And we want what is there at index two.
So that should give us the node three because this is zero one and two.
This is two right.
So this is index two.
So we're getting node three which is correct.
So we have the value three which is correct.
And its next is null.
So this is working.
So the get function is also working.
Now if we have get and then we are adding an index like ten then we should get minus one.
Yes we are getting minus one because this is an invalid index.
All right.
So our functions are working.
Now let's go ahead and test delete at index function.
So let's say we want to delete the head.
So we could delete the first node over here.
So let's go ahead and test that over here.
So we can say SL dot delete at index.
And we pass in the index zero.
Now we are getting back the node which we are deleting.
So that's what we want to do to do right.
So we are delete.
We have deleted the node with value one.
So we're getting this back.
Now if you want to take a look at the singly linked list let's do that.
So over here you can see the size is two which is correct because we have removed this index over this
node over here.
And the head has value two and the tail has value three.
So two is pointing to three which is correct.
Three is pointing to null.
All right.
So this is working.
So we have tested all the functions and it's working.
Now let's take a quick look at how the code is working and walk through it.
So first we take a look at the get index function over here.
Now let me just grab a pen for this.
All right.
Now, let's say over here, we have this singly linked list already.
And let's say the index which is passed is equal to two.
So we want to get the node at index two.
So first we check if the index is valid.
So we can see over here the index is valid right.
So index is not greater than equal to size which is three size is three.
So the index is not invalid.
So we continue and we have the counter equal to zero.
And then the current is going to point at this head this node over here and then counter is zero and
index is two.
So they are not equal.
So we're going to move current ahead.
So current is equal to current dot next and counter is incremented to one.
Again we check is one equal to two.
No that's not the case.
So current is increased is moved ahead to current dot next and the counter is incremented to two.
Now we check is the counter equal to the index.
Yes two is equal to two.
So we come out of the while loop and we will just return this node over here.
So that is how the get function works.
All right.
So let's go ahead and look at the other possibilities also.
So we go to the next function.
Now let's take the function added head and see what's happening.
So let's say we want to add a value for at the head.
So let's take that example.
So the value let's say is passed as four.
Now we will go ahead and create a new node with the value four over here.
And then we are checking if the head is null.
The head is not null right.
So there are already nodes in the singly linked list.
So we come over here and we are going to say node dot next is equal to this dot head.
So this is the current head.
So we are making this connection over here node dot next is equal to this dot head.
And then we are saying this dot head is equal to node.
So we are making this node over here as the head.
And then we are incrementing the size.
So the size is incremented to four.
And then we are returning the whole singly linked list.
So return this will return the whole singly linked list.
So this is how the added head function works.
Now let's take a look at the other functions which is added tail.
The next one is added tail.
So let's say again we come back to this singly linked list over here for the example.
And then let's take a look over here.
Let's say again the value is something like seven.
So we have the seven over here.
So we come over here and we create a node with value seven.
And we check if the head is null.
The head is not null.
So we come over here and we are going to say this dot tail dot next is equal to node.
That is this is the current tail.
We are making this point to the node over here which is seven.
All right.
And then we are saying this dot tail is equal to node.
So this over here is made the tail.
And remember, this is already pointing to null because that's how we have written the node class,
right?
So this will already be pointing to null.
And this connection is removed because we are making the previous tail point to this node over here.
And then we increment the size and we are just returning the whole singly linked list.
So this is how it works.
All right.
Now let's go ahead and look at the next function which is to add at a particular index.
Again let's take this as the uh sample singly linked list.
So let's just scroll down.
So we have our added index function over here.
Let's say we want to add at index um one which is this over here zero one and two.
All right.
Because we have already discussed adding at the head and the tail.
So that's why I'm taking index as one.
And let's say the value is equal to seven.
So first we are checking if the index is valid.
Now the index is valid.
Now over here we are checking if the index is equal to the size which is three.
Right.
So if the index was equal to three we would have just called the added tail function.
And we have already discussed that function that would have that function would have added the value
the with seven as a node at the tail.
Now if the index is equal to zero, we are just calling the added head function.
And this node over here would be added at the head.
Now over here it's not the condition.
So we continue and we create a new node with this value seven.
So we are having a node with value seven over here.
And then we are going to say prev is equal to this dot get index minus one.
So index is one.
So one minus one will give us zero.
So we are going to get the node at index zero which is this one over here.
So this is going to be equal to prev.
And then over here we are going to say temp is equal to prev next.
So temp is this node over here which is prev dot next.
And then we are going to say prev dot.
Next is equal to node right.
So this over here is going to point to the node which is seven.
So we are making this connection and then we are seeing node dot next is equal to ten which is this
connection over here.
Right.
And this connection is removed because we're seeing PrevNext is equal to node.
All right.
That's it.
So now one will point to seven.
And we can see seven is pointing to two.
And two is pointing to three which is pointing to null.
In this way we have inserted the node with value which is passed over here at the index which is one.
So you can see now this node is at index one.
All right.
And then we are incrementing the size and returning the linked list.
So this is how this function works.
Now let's take a look at the last instance method that we have written over here which is the delete
at index function.
So again let's take this as the sample linked list which we have.
And let's scroll down.
So we have this function over here.
Now what do we do?
Let's say we want to delete at zero.
If you want to delete this then it's going to be happening over here.
So we see that the index is equal to zero.
Then we will make this the temp.
So this is going to be temp.
And then we'll say this dot head is equal to temp dot next.
So temp dot next is this node over here.
And we're going to make this the head.
And then we decrement the size.
And if the size becomes zero that is if this was the node with just one node right.
And then we are deleting this node over here.
And we made this the head.
So in this case head is pointing to null.
We also need to make tail point to null right.
So that's why we have this check over here.
So if there was just one node over here then after removing this the size will become equal to zero.
And in that case we will make the tail also equal to null.
And then we are just returning temp which is the node that we have deleted.
So that is how this part over here works.
Now if the index is not equal to zero, let's say we wanted to uh pass an index which is equal to for
example in this case two.
So let's say the index is passed as two.
All right.
So we have the index two over here now 012.
That's this, this uh part this place.
So we actually want to delete the tail that would be coming to this part of the code.
Right.
So let's take a look at it.
First what we do, we are going to store the old tail.
We're going to store the old tail because we want to return it over here.
Now.
Old tail is going to be the tail at the moment, which is three.
So this is three.
And then we are going to calculate the find the new tail which is the node at index minus one.
So index is equal to two.
And we have seen that we came over here which means that index is equal to this dot size minus one.
So we just need the previous node which is get index at index minus one.
So which is equal to two minus one.
This is two minus one which is equal to one.
So we are going to get the node at index one which is this.
So this is going to be equal to the new tail.
All right.
So again at this point it's just a variable.
And now we have to set this to the tail.
So for this we are going to say this dot tail is equal to new tail.
So we are making this the tail.
And at the moment two is pointing to three, but we need the tail to point to null.
So over here we are saying new tail dot next is equal to null.
So we are making this connection over here.
So this connection is removed and it's pointing to null at the moment.
And it is.
And in this way we have removed the node at the tail.
Now we just need to decrement the size.
And we are returning the old tail which is this node over here.
So this is how deleting at the tail works.
Now let's continue.
Now if we are deleting a node which is not at the tail and not at the head, then what do we do?
Let's again take this same example over here.
And let's say the index which is passed to us is equal to one.
So this is zero, one and two.
Let's say we want to delete this node over here.
So we see that it is not the head and not the tail.
So we will come over here, right?
So we will come to this part of the code.
And then again we go ahead and find the previous node.
For this we use the get function which you have already written.
So we get the node at index minus one.
So that's going to be this.
This is the pref.
This is pref.
And then we have the deleted node because we want to return it right.
So we're going to store this particular node which is private dot next.
And then we're just going to say prev dot next is equal to deleted node dot next.
So deleted node.
Next is this one over here.
So we are saying prev dot.
Next is this.
So we are making this connection.
And this connection which was there previously is removed.
In this way this node is deleted.
Then we reduce the size and we can just return the deleted node which is two.
So this is how the delete at index function works.
So we have taken a look at all the functions which we have written.