All right, welcome to the coding interview, question reversed, not in Grove.
Here's the problem statement, given a link list, reverse the notes of a list came at a time and return
its modified list.
Care is a positive integer and is less than or equal to the length of the linked list in the number
of nodes.
Is not a multiple of K then left out, not in the end should remain as a bridge.
All right.
This is the overall problem statement, if you're given this particular linked list and Keiko's to two,
then we have to reverse this part, then this part.
All right, here, one will go here and two will go here and for these three will go here and four will
go here.
All right.
And it will look like this, OK?
And this is the linked list that we should return.
All right.
If you're given this particular link list and chemicals to three, then you have to reverse this part.
And here we don't have three nodes.
Right.
So we have to do nothing here.
So it will remain as it is.
And if we reverse this part, then it will look like this three to one, four, five now.
OK, now how we can solve this problem.
This is one of the hardest link list calling into question.
Now, let me go through the intuition to this problem with the help of pseudocode.
First, I'm going to declare a function, reverse of the takes, head up and give a link list and an
integer K for testing support.
Let's add you were given this link list and cakehole to talk then.
We're going to check if Hillary Clinton to talk to one, then we're going to return the hate.
All right, then we're going to create a new link list with the value zero.
All right.
And this linked list of fake will contained only one value and there is zero.
Then we're going to add the head to this, not zero.
OK, then it will look like this.
So this is a linguist and hear this pick point and point to this note, zero.
Then we're going to declare another point of pride that will also point to this hate dero.
All right, then we're going to declare a variable like close to zero.
Then we're going to declare another pointer P that will point to the hit.
So in this case, it will point here at this node.
After that, we're going to run it live.
Well, P Noriko's two now.
All right.
Here, let's update this value I close to eye plus one.
So in this case, it's become one, right?
And here you got to choose one model as two is not equal to zero.
So this part will not run, then this part will run Picards to the next.
So in this case, people point to this, not to.
All right.
For the next iteration of this, well, live, I becomes too right, so too modulatory records to zero.
It's true then praecox to reverse Pre and Piddock next.
So we're going to call this reverse function with the head dero.
And next predict makes call to this node over here.
Right.
Then we have this function here.
It takes two arguments and next then we're going to declare a point, a lost that will point to predict
next.
So it will pointier.
OK, well, then we're going to declare another pointer, lost that next.
So current Will Pointier.
All right, so let's assume this is our current and last the next is current here, right?
And last the next, according to this note.
OK, then we're going to run a live well, current nautical through next then last lasted next to got
to current to the next Viniar.
First, let's disconnect this so it will connect to this node.
Right.
Because last dot next to this point it all points to current dot next to current and next is this not.
Now we see that last look next week, oldster current dot next to it will point to this Node three,
then current next week to predict next.
So current DOT next, let's disconnect this pointer and let's move to predict next to it all points
to this node.
Then predict next, according to current Supreme Court next in this case, let's discuss this and predict
next EcoStar to current.
So it'll also point to this, not that we see here, is that this fake link is right.
So now it will be like this zero two one three, four, five.
All right.
So we see that it's modified.
All right.
Here we see that this two not reversed.
Right?
We see here, Deyrolle, two, one, three, four, five.
OK, so it will look like this.
Now we see that we reversed this to node two and one.
So very close to zero two one three, four, five now.
All right, then it will look like this.
So these two are not reversed.
Right.
So two one.
Here is the pic linked list is zero two one three, four, five.
And this reverse function return this node.
Right.
So in this case, pretty close to one, three, four, five.
So here you see these three equals two one, three, four, five.
So three will point to this node here.
So here people point.
OK, then, because to predict next P will point here right at this node three.
All right.
Then at this current position we have equals to two.
Right.
Then for next iteration it becomes three.
So in this case, point to this node for for this current iterations, it becomes four.
Right.
So in this case, for the last two equals to zero.
So that's true.
So we're going to call this reverse function play.
So pretty is pointing to this node one and predict next.
And this note, OK, then we're going to declare the pointer lost in current.
So last two point predict next.
So right here.
Ill and currently holds the last word next to current viewpoint point right over here, see?
Then we see a last ditch next to go to current dot next hour at last, up next to go to the current
next.
So there's no point here and this will be disconnected from here, right?
OK, then we have got into the next record store in this kid's current next records to predict next
to that.
Is this not so?
It'll point to this node right over here and this will be disconnected.
Then predict next records to current.
So current will point predict next, so predict next year.
So current also will point here.
OK, then we see predict next equal to current.
OK, here we see predict next records to current.
So this link will change.
Right.
So this will be disconnected from this node.
So it will connect to from this node to this node for.
All right.
Like this.
OK, then you have current codes to last dot next.
So kind of a change this pointer from here to this node, OK.
So finally, our linked list, it will look like this.
So we have modified our linked list.
So in this case, this reverse function will return this note.
And so pretty close to in this case, we have three and five for this iteration.
We have I got to four.
OK, then P will point to this node five right here.
And for next iteration, we see we have current node equals two now.
Right.
So in this case, it will stop here and we're going to just return the favor next and fake dot next
each this node over here.
Right.
So that means we are going to return here to then we have linked to two.
Is this not one?
Then we have connected here for one four.
Then we have we three.
Then we have here this five.
And then we have now.
OK, so we see that we have reversed the legalist list group at a time, so this group first, then
this growth.
All right, so two one, then four, three, five, then now.
OK, so that's how we can solve this problem.
And this is the core intuition to this problem.
OK, in order to understand this problem, go through with your own example, then it will be clear
to you.
And here I show you how it's actually working.
Right.
OK, so this is how we can solve this problem.
And this is not an easy problem, right?
If we don't get it, don't worry.
I realize this video, the solution will take bigger of in time complexity where it is the length of
the given linked list, and it takes constant space complexity.
All right, guys, this concept was clear.
Epiphany, Christian epiphany, suggestion.
Let us know.
Thanks for watching this video.
I'll see you in the next video.
Till then, take care.
OK, so it will look like this.