Hi everyone, let's do this question.
Create a doubly linked list class.
Write instance methods for this class to be able to one.
Remove a node when the node to be removed is given as input.
All right.
So over here we need a method.
And the input will be a particular node.
And this function should remove that node.
And the second part of this question is insert a node before a particular node.
So we should be able to insert a node before a particular node, both the node to be inserted and the
node before which the insertion is to happen will be given as input.
If the node to be inserted is part of the linked list, then shift its place to the desired location.
If the node to be inserted is a new node, then insert the new node at the place desired.
So this is the question.
Now we will solve this in two parts.
First, let's do the first part.
Now when you're doing this in the interview remember feel free to ask any clarifying questions so that
you are very clear with the question.
Now a particular one of the particular questions that you could ask is for removal.
What if the input node is not part of the linked list.
So for example, you have a linked list one which points to two, and these two point to null.
So this is a doubly linked list.
Now let's say we are calling the remove function which we write.
And the input is another node which is not part of the linked list.
Right.
So what should happen now?
The interviewer replies then no removal happens.
All right.
So if the node is not part of the doubly linked list then removal does not happen.
All right.
So this is a pretty straightforward question.
So let's go ahead and write a few test cases to understand the question better.
And again we are only doing the first part.
We will look at the logic and we will code it and walk through the code.
And then we will come back to the second part.
All right.
So let's look at a few test cases.
Again test cases are just different inputs and what the expected output is with respect to those inputs.
Now let's say we have a doubly linked list.
This is the one.
So we have a node with value one pointing to this node over here which has value two.
That's pointing to this node over here which has value three.
And that is pointing to a node with value four.
And again it's a doubly linked list.
So it has two pointers.
Right.
And then these two the head and the tail point to null at the edges.
All right.
Now let's say the input is node two.
Now what is the expected output.
We need to be able to remove this node.
And that means that the doubly linked list will look like this.
Now right.
We will have one.
Then we'll have three.
Then we'll have four.
So we have removed this one right.
So this is what we need to achieve.
So it's a pretty straightforward case.
And again we saw the edge case.
Let's say this is the doubly linked list.
And the input is another node which is five.
And let's say it's just pointing to null and null.
So it's not part of the doubly linked list.
So in this case nothing happens.
Right.
So that's the question.
Now let's go ahead in the next video and discuss an approach with which we can solve this question.