Hi everyone, we have successfully coded the solution for this question which we have discussed.
Now let's take a sample input and walk through the code that we have written, especially through this
function over here.
Now this code over here, over here we have class node and then this part over here.
This is written so that we can test the function that we have written.
Right.
So when this code executes we will get a singly linked list right.
Singly linked list.
And this is how it looks.
We have one which points to two.
Which points to two which points to a node with value three.
And over here the last two nodes have a value of a right a character a.
So we have just made five nodes and then we have connected them using Dot next operation right over
here.
So this is the singly linked list.
And the head of this singly linked list is this node over here.
And that's what we are passing to this function over here.
All right.
So again here we are calling the function remove dupes.
And then we are passing the head.
All right.
So once we are inside this function we set the curve value to the head.
So curve will point to the head at this point.
Now car is coming truthy because it has a node right.
So we are in the while loop.
We go inside it and this will keep repeating till current comes to null at that point.
While car this part, when it is null, it will become false and we come out of the while loop and we
return the head.
So that's what's happening inside this function.
Now initially over here we can see we are setting next distinct val as car dot next.
So at this point this is next distinct val.
So I'm just writing next over here to keep it short.
All right.
So using this line of code we are pointing next distinct val to the next node which is this one over
here.
Now over here in the while loop inside we are checking whether these two are of the same value or not.
Right.
So we are checking that over here car dot val equal to equal to equal to next distinct dot val right
next next distinct val dot val.
So over here we can see that they are not equal.
So we don't go inside the loop.
And over here we do car next is equal to next distinct val which is already two.
In true in this case.
So nothing is changing.
So this is actually required only when the case is where we had some nodes to delete.
Right.
But again over here we don't have anything to delete.
But so this is the already existing situation.
So nothing changes.
And we set car is equal to next distinct Val.
So car comes to this part right.
So over here car points to the second node.
And car is still true.
Right.
So it's it's truthy.
So again the while loop is executed and we set next to the next node.
Right.
And we are going to compare these two.
Now in this while loop we are going to compare them.
And they are having the same value.
And next distinct val.
This one over here is not equal to null.
So we come inside right.
So we come inside and we do.
Next distinct val is equal to next distinct val dot next.
So we move this one step ahead right.
So we come over here again in the while loop we check whether these two have the same value right.
So this will not be true.
Therefore we come over here right.
Therefore we come over here and we say car dot next.
That is the next from this node is equal to next distinct val.
So we make this node point to this one over here.
All right.
So that effectively deletes this node.
Right.
So two is now pointing to this node over here.
So we could keep doing that again and again.
Right.
So in this case also we will make this node point to null.
And finally when Kerr is equal to null this while loop breaks and we come out and we return the head
right.
So that's what's happening over here.
And uh, as we have discussed, this has a time complexity of O of n.
And why is that so.
Because you can see we are just looping or we are just traversing.
We are only traversing the singly linked list only one time.
So that's why the time complexity over here is O of n and the space complexity is O of one, because
we are not using any additional space.