Hello all and welcome back.
So in this assignment you have to solve the question.
Delete a note at an index.
But we have to do the same recursively.
Right.
So we have done it using iteration.
So the flow is going to be the same.
Let me draw a linked list.
Let's say this is our linked list for the time being.
Everyone now in recursion again we have to just follow the framework.
So first is the base case.
Base case I believe is very easy.
Like if head becomes or if head is none like you are giving me an empty list, then how can you expect
me to delete anything, right?
If head is none, how will I delete anything?
Again, think in terms of recursion just right.
You have to make sure that you are handling some part and else recursion will handle right?
Okay.
Second is going to be.
Your recursive call.
In third becomes r work.
Right now in this if we pay attention everyone.
So recursive call is pretty easy.
Again we are just going to call recursion and just make index minus one.
But I think again we can do our work first.
What if you are asking me to delete the first node like the index is zero, then I think we have already
seen in that.
So this is going to be our work.
If index becomes zero then we will simply return head dot next, right?
We have already made sure that head is not null.
That's the base case.
If index is zero then we will delete it.
And if that is not the case then okay, let recursion do the work.
Head dot next is equal to delete head dot next N index minus one.
Right.
So yeah, just this is how we can handle it.
Again, these are interchangeable.
Till now we have done lots of problems on recursion.
So I'm pretty sure that you will be able to understand why each and every step is done here.
So let me do this everyone now.
Define delete at index recursion.
Right.
I will be getting an head and an index.
Right.
If my head is none I can just simply return none.
But along with that, I should tell that hey, the index is out of bounds.
Index is out of bounds.
Great.
So once that is done, then maybe we can move on to the next case.
Let us do our work first.
If you are asking me to delete the first one, I am just going to return you head dot next.
Now here we have already made sure head is not none.
So we don't have to worry if this will give an error, right?
If that is also not the case, then I am just going to use recursion.
Okay, so I don't need to worry on anything.
Recursion is going to do all the work for me.
So head dot next and index minus one.
Once recursion does the work I can just return head.
Okay cool.
So yeah I think this is what we can do.
Let us just test it out once.
Hmm.
Yeah.
Maybe we can work with this.
Great.
Here, let us run this.
1020.
30.
40.
50 minus one.
Okay, so, yes, we are able to delete this part, right?
Let us check this for an empty linked list.
Index is out of bounds because we are giving head as null and we are asking for three.
Now one thing again to notice here everyone, even if the index is zero, right?
Even if you want to delete the head, but head by default is none, then of course you cannot do anything
right.
If head is none, then it's an empty list.
So please make sure that you handle this in a similar way.
Now next part, which we should test as well is this question right?
So ten 2030 minus one.
If you remember we faced an issue in the iteration part here which we solved by this condition.
Will we face the same problem here.
Let us see.
Okay.
No this seems to work right.
This seems to work.
So let us understand how this is working.
Everyone, this is a lot important to understand once.
See again, just make the recursive calls and let me draw the linked list vertically this time, so
that I can tell you what the recursive call going to be ten, 20, 30.
And right.
See the very first way, the very first I am getting my head as this and index is three.
Make sense?
Next I am getting my head as two And next I'm getting my headers.
Oh, sorry.
Headers.
20 index at two.
Head as 30.
Index as one.
So again all these are still not done.
And I reach this condition.
In this condition my head is none.
Index is zero.
Then again if you think about this, if I just give you an empty linked list with the index zero, of
course you cannot do anything right.
It's an out of bound.
You can just print.
So this condition is going to get hit first.
In the iterative part we have to make sure that we handle that because we were handling everything in
this recursion is doing the work.
Plus this condition, the way we have handled this, that is making all the difference, right?
Because in this particular case you are passing none.
And if index becomes zero okay, it kind of signifies already that, hey, we seem to be somewhere,
okay, the link is empty, so you cannot delete anything, right?
So that's why we are getting that answer.
And again, even if it is on tail right.
Even if it is on tail when we call on this particular, let's say this is the tail, right?
30 is the tail.
So that was where the major problem was happening in the iteration part.
Right.
But in recursion we actually call on tail and it is none.
Right.
So that's why it gets taken care of.
In this case when the head becomes none and index becomes zero again, if the index will be greater
than zero, that is one, two, three something.
Then again we are going to do the same thing.
Okay everyone.
So I hope this is clear.
Again that was just one caveat which was there in the iterative part as well.
So yeah just let me clear this.
Great.
So we can just quickly do it once ten 2030 minus one.
It's giving me the same thing.
Index is out of bounds after deletion.
The same list is there.
Okay.
So great everyone I hope you were able to do the same again.
Please make sure that you follow the framework and try to do these questions.
These are the kind of questions which are normally asked.
Uh, moving forward we are also going to do one very easy but medium level question on, uh, on your
LeetCode, which is based on the same principle.
Okay.
Thank you.