Hello all and welcome back.
So in this video let us continue our discussion on Nyquist.
And now let's correct the take input function which we wrote in the last video.
So if you remember, we were facing an issue where we were not able to connect this intermediate nodes
in our linked list.
Right.
So we saw that take input was failing as we failed to connect last node to the newly created node.
Right.
So whatever the node was getting created it was getting created to our head instead of the last node.
So let's say for example say I have this particular node which is created.
So let me make this new node.
Now my whole aim should be that instead of creating it with head right, instead of making this particular
connection, I should rather travel to my tail or the last node and then connect this.
So this is what I should be aiming for.
I should be able to remove this.
Let's say the value is 30.
Here and say this is 40.
and I should be able to make this particular connection.
Okay, so let me make this new connection in green so it is distinguishable.
This is how my connection should be right now.
Next challenge in this line is that I have to somehow from my head, go to my last node and I know the
property in my last node.
So last node is the one last node.
Is where.
Next is null right.
Next is null.
Great.
So now with this knowledge, since we are able to now visualize this knowledge and let me show you this
as well in Python tutor once.
So see when we were here this is the last node right.
So we have to start from my head and make sure that I'm able to go to my last node and then connect
this.
Right.
So let us see how we can do the same in code.
First, so this is the code which we have written the last time.
Here was where the mistake was happening.
So what we should do is instead of doing this we should first create a temp and head.
Now again this is very important.
Something which we saw in the print linked list function.
Right.
We cannot again lose our head.
If we lose our head then we will not be able to return it properly.
Right.
So once I created this temporary variable, then what I have to do is that run a loop while my temp
dot next is not equal to none.
And once this loop is actually finished, I can just say that hey, go to the next one till you get
this.
And once this loop is finished, I can be sure that I reached out to my last node, right?
And once I'm able to reach out to my last node, then I can just do the connection.
So this should just handle the same thing, right?
So let us quickly try run it once everyone so that we are able to understand it.
So let's say this is my list 1020
3040.
And say I have to join this 50 node.
Right?
As of now this is pointing to null.
So I'm going to start my temp.
So this temp is going to start from here.
Let me use a different color.
Yeah.
Temp is going to start from here.
Now is my temp dot next null.
I don't feel so.
So I'm going to go to the next one again is my temp.
Next null.
No.
So let's go to the next one.
Is my temp dot next null.
No.
So let me go to the next one.
Is my temp dot next null.
Yes that's the case.
Okay so one thing.
What everyone I will be using null and none interchangeably.
Both would mean the same thing.
Actually in languages like C plus, plus and Java we have null.
And in Python we have none.
Okay, so the understanding is the same.
So now once I have reached to my tail node, the last node, I am just asking it to get connected to
this particular thing, which in turn is pointing to null, right?
So this is what I am doing.
And let us try to run it once.
And then of course we are going to see the same visually as well.
So after making this particular change, if I run my code now, let me enter ten, 20, 30, 40 and
50 and minus one.
Okay, so this is the linked list which I am creating every 110, 20, 30, 40, 50.
Uh, just this one.
Exactly.
Just I am having a 50 node as well.
So now if I run this now if I run this, see what has happened, I am able to see my proper list.
Right?
So linked list is getting created properly.
So great.
Glad that this worked.
Let us now go back and see the same in one second.
Yeah.
So let us go back and see this in your python tutor as well everyone.
Let me edit this.
Code it.
So yeah, now it has basically been compiled.
So pay attention everyone.
I'm just running it first.
All the codes or sorry, all these functions are going to get initialized.
And then when I call this take input function it's going to ask me for a value.
Let's say the first value I give ten.
Right.
So for ten we know that as of now linked list is empty.
It's going to create a head.
Then going forward I have to check while value is not equal to minus one, I don't feel the value is
minus one.
Here it's ten.
So it's going to go forward.
I'm going to create a new node.
The new node is actually ten with next as none.
I'm going to check if my head is equals to none.
It is true.
So I'm going to make this my first node right.
See this is my first node.
Now again for the next node I can do 20.
And it's going to create a new node with 20.
You will see one thing that this new node connection is going to break.
And I'm going to now then finally push here.
Right.
So see new node is now basically pointing here.
I'm going to check it again now.
Okay.
So if it is none no.
Now if we see this is the last node right.
This is the last node.
So this particular condition or this particular condition temp is equals to this one one second.
Yeah.
This particular condition if you see temp dot next is equals to none.
This is getting true here right.
Because this is the last node.
On that note let me just move this.
Yeah.
If you move forward then new node is this temp is pointing here.
Temp dot next is none.
So it's not going to move forward.
And I'm going to join it here.
So we have just changed how we were joining our nodes right.
Earlier we were just directly joining with it.
Now I'm just joining it by making sure that I'm on the last node.
So now let me enter 30.
Now again let it compile.
So for 30 again it's going to make a new node okay with 30.
But this time because we have connected our code, what's going to happen is that instead of directly
joining our head this to this, it's going to go to this temp.
Right.
So pay attention everyone while temp is dot next is not equals to none.
I feel that as of now it is not equals to none.
Right.
So temp is pointing here.
So see the pointers.
This particular thing is not pointing to none as of now.
Right.
Again everyone.
So in linked list or linked list in general the visualization is very important.
Given any question, any problem, you have to make sure that you draw the linked list and then just
see that how and where everything is pointing.
If you do this, 90% of the questions can actually be solved pretty easily.
Okay.
So let us move forward now because temp is not temp, dot next is not null.
Temp is going to move to the next node and see how the pointer changes.
Right.
Let me just move this for the time being.
So this is a dangling thing.
New node.
Now because this pointer has changed okay.
If I move forward m dot next.
Now this one will basically get in its next the 30 node and we can see the same right.
See how the linked list is now basically getting joined.
Now let me add 40.
Okay so let's see new node.
And yeah new node is getting created.
This is the 40th node like the node with 40 value okay.
Again I'm going to start and make my temp is equals to head.
So it is here right now head is not equals to none.
So let us go forward.
Temp is head.
And I'm going to now move my temp right.
See.
Temp is getting moved.
And now because it has reached to the last node.
And I hope all of you are able to now actually visualize this the way I'm showing and teaching you,
because I want to make sure that you understand this.
Right.
So now, because it is here, it's going to join this particular connection.
This connection is going to get made and see this is the linked list which is getting created right.
So see how everything is so nicely visible.
Let us start at the last one which is 50.
Okay.
Now again the whole thing is going to happen again.
New node is going to get created.
I think we have added lots of nodes.
So see 50 is getting created here.
The code and everything is same right.
Because it is within a loop.
So that's some good thing which we have done.
Now again new node is going to get created.
Temp is here.
We have to go till 40.
So based on the condition temp is going to jump to each and every node.
And finally when temp dot next is none, we have to just join this and let me enter minus one now.
So minus one will imply that hey stop the loop.
Now stop the loop.
Right.
Once the loop stopped I'm going to return head.
Now pay attention everyone.
All this because it was just created in the local scope of my function gonna get removed.
Okay, but because I'm just returning this reference to my new head here, only this will remain and
I will be able to see my whole list.
So let me just go to the next and see what's going to happen, how nicely we are able to see.
Right.
So new head, everything is just connected.
If we lose out on lose out on anything in between, we don't have the reference, then it's going to
get deleted in Python.
Okay, so this is the whole thing now everyone.
Now again we can see how it's going to get printed.
So in the print again we are going to make a new temp.
Uh, one thing more.
Don't get confused on this temp and this temporary right.
So they are totally in different functions.
Uh, this temporary will be in the print linked list function.
So they have no relation like absolutely no relation between them.
Okay.
So I'm pretty sure the local and global scope video you have seen already.
If not please go and revise that once.
And now when I'm going to just run the print function C temp is going to get initialized.
First it's on head, It's going to print the head thing.
So we let's see the output.
Yeah.
Then it's going to go 20.
It's going to print 20 then 30, then 40 then 50.
And finally we can return.
Right.
So just an extra line because of this now.
Temp is none.
We have to go till temp is not equals to none.
Okay.
Once temp is done we come out.
We just print an extra line and we are returning.
You will notice one thing.
The reason we use temp is that still our head is pointing here.
So any other operation which I want to do in this particular function, I can still do because I have
my linked list.
Okay.
Always remember that if you have your head, you have your linked list.
If you lost that, you cannot do anything on your linked list, right?
Because there is no way I can travel back.
So for example, if you ever, let's say if you make your head point here instead of this, you cannot
go back.
There is no way you can go back, right?
You cannot go back to this particular direction.
And that's the reason.
That's the exact reason that we should always save our head.
Okay, great.
So now let us move forward and we can see the whole output.
Okay everyone.
So great I hope that was clear.
This is the whole proper code which we have written.
Uh, I will just request all of you to do one thing now, which is to find the complexity of this function.
So this take input function which we have written.
Right.
You have to find the overall time complexity of this particular function okay.
So I will write this down as well.
What is the time complexity.
Of take input function okay.
So make sure you calculate that.
And in the next video we are going to see if there is some way we can correct the same or improve the
same.
Uh, basically.
Okay.
So cool.
Then everyone let's meet in the next video.
Thank you.