Let's now go ahead and do the second part, which is insert a node at a desired position.
The node and the position are given.
The linked list is zero indexed.
If a given node is a node existing in the linked list, shift it to the desired position.
So this is the question over here.
Now the difference between the previous method which we have written, which is insert before was that
at that point the position was specified in terms of a node.
But over here it's going to be a position something like zero or 1 or 2.
And then it's also mentioned that the linked list is zero indexed.
Now if you're getting this question in the interview, ensure that you ask sufficient clarifying questions.
Now a possible question could be if a linked list has only three indices, and if position is given
as ten, what should happen?
So that means there are just three nodes.
And this is the linked list.
Now it's a doubly linked list.
So it's going to point in both directions.
So we have both directions over here.
And then the position is mentioned as ten.
So what should happen now?
Let's say the interviewer says in that case insert the given node at the tail.
All right.
So let's go ahead with that.
And another possible question could be what should happen if the doubly linked list is empty and a node
is given to be inserted at a position?
Now let's say the interviewer replies and says to you that in this case, just make that particular
head the the just make that particular node as the head.
All right.
So let's say this is the clarification received.
Now let's go ahead and write a few test cases.
Now we have two possible scenarios.
For example let's say this is the doubly linked list.
And let's say position is given as zero and the node is two.
So you can see over here that the node is part of the doubly linked list.
So it's mentioned that in this case we have to just shift its position of this particular node to the
given position.
So the output has to be 2134 and two would be at the head.
Right.
Because this is moved to position zero.
And this is zero right.
This is 012 and three.
Now let's say another possibility is let's say position is given as three and the node is still two.
So in that case the output should be 134 and two.
So you can see that you have 012 and three.
So the given node which is two is moved to position three.
Now let's say the position is mentioned as two and the node is still two itself.
The output which is expected would be 132 and four.
And you can see over here you have zero one and two.
So the node two is at index two.
Now these are the possibilities where the given node is part of the doubly linked list.
And then this is the case where we are shifting to the head.
And then over here we are shifting the node to the tail.
And then this is a case where we are shifting the node to somewhere in between.
So these are three possibilities.
Now you can also have possibilities where the given node is not part of the doubly linked list.
For example, let's have this node over here which is not part of this doubly linked list.
And then let's say the position is given as zero.
In that case you would have to make it the new head.
So you have 51234.
And then both of these extremes are pointing to null.
And then another possibility is let's say the position is mentioned as three and the given node is five.
So in that case the output expected would be one, 235 and four.
You can see this is position 3012 and three.
So you have five at position three.
So we have understood the question and we have written a few test cases.
Now let's go ahead and discuss the method by which we can solve this question.