Hello all and welcome back.
So in this video let us continue our discussion.
And now let's do the last operation which is searching in our linked list.
So we are going to start and do the searching by value.
So given a particular value you I will have to return if that value is present or not.
Or better we can just return the index.
So if the value will be there we will return the index something which you have been now doing a lot.
So pretty sure we are comfortable with that.
If not, then I have to just return the.
Let's say we are returning minus one as an identifier.
Okay, great.
So let us draw a linked list.
And see this is none.
Second.
So this is our linked list with five nodes.
Now say I have to find value which is equals to three.
Now I know what different nodes are.
So 0123 and four right.
So these are my nodes everyone.
Now to find this particular value of course we are going to run a loop again right.
We have to start from head and check each and every node.
If we are able to find that particular value then we will just return the index.
So we can have a index which is starting from zero.
If we are able to find then we can return index.
If our loop due to some reason fails, then we just have to return.
So it is very much similar to I would say linear search right?
It is similar to linear search, something which we have already done, just that we are doing the same
in an linked list this time.
Now we all know linear search has a complexity of O of n here also complexity will be O of n as we have
to take worst case where either the element or the value is in the last, which in this case will be
five or it is not present at all.
Right.
So great everyone.
Now let us try to code this in uh, one second.
Yeah.
So I have just actually added one thing here.
This is our commons file.
What I have done is I have created a new function create linked list from list.
Okay.
So instead of entering the values what I will be doing I will be passing a list and just these values
will be used.
Okay, so just to save ourselves the pain of always entering the input, which is normally not feasible
in many settings, right?
On any let's say in online compiler or somewhere, if we have some issues.
I have just made sure that via list also my whole linked list can be created.
Okay, so this is in my common dot p y.
This is a file where.
Now I have imported this particular functions.
So just to check we can have head create from link create linked list from list 12345.
Okay.
And let's say print L head.
If I run this, we are able to see.
Let me run this again.
So yes we are able to see our output.
Right everyone.
So this is the list which I sent.
And we are able to see the same.
Great.
So with that out let us now write.
Try to write the code for search.
Right.
So def search by value.
We will be given the head of our linked list and the value.
Great with this.
Now again we have to think a little bit, right?
In our case, when we were doing linear search we had the length of our array or list right here.
We don't have that, but instead we have an identifier.
And that identifier we know is that last is going to be none.
right?
As a good habit again, we will not be using head.
We will be making a copy.
Right?
So that is.
That is something which we have been doing since long now.
So instead of making head, I will be starting with a copy of head known as temp.
Right.
So let us do that.
Temp is equal to head index I will take zero.
So I will start from zero.
Keep on increasing it as I go to each and every index right.
So while my temp is not equal to none, this is the condition I should first check.
See, as of now I have not checked for my head for my first node.
What if that is only equal to the data, the value which I am looking for.
So first I should check if temp dot data is equal to value.
Then I should just return index right?
So this will be something of a base case in our recursive call as well, not a base case.
The work which we have to do, but I hope the invitation is well.
You are able to understand why am I doing this first?
The reason if I'm on the first node altogether, I have to check this.
Then I think the work is pretty simple.
Temp is equal to temp dot next and index plus equals one.
So I will be just now moving to each and every of this node index will keep on increasing.
And if I'm able to find then I should just make sure inside this line inside this particular case that
I will return.
If not, then maybe I can say not found.
Okay, so pretty straightforward function.
Let me let us just try it everyone.
I hope this is clear pretty straightforward.
Just we are doing linear search in a linked list right.
So searching and let's say print I can just directly print it as well instead of catching it somewhere.
So search by value know, it's giving the head and I'm giving the value as three.
Okay.
So great everyone let us now try to run this once.
Let us clear this all.
Okay I think I zoomed in or something.
One second.
Okay.
I mistakenly zoomed in.
Great.
Sorry.
Let us clear this.
Yeah.
So.
Yeah, let me run this file.
Overall, we don't have to give any input now.
So 12345 is our linked list okay.
And we are just printing.
Uh, let me just print linked list as well so we can see the linked list in front of us.
Great.
So now if I run this we are able to see 1234, five searching.
Okay.
I think we have not passed the value.
My bad.
Let us pass three.
See, it is able to give the two as an index.
So 0122.
Is the index right everyone?
Uh, let us do 30.
It is giving not found.
Now let us just check for the boundary cases as well.
So first is the one which is our head.
Uh, the head which we have.
Right.
We are able to see that head.
It is able to give.
Okay.
Next one which we normally check on the tail.
Yeah.
See it is able to give tail as well.
Right.
So this is working fabulously well everyone.
Now actually we can write the recursive one as well.
Maybe you can do the recursion the next time.
So recursion is going to be a lot easy here.
Search by value recursive.
Okay.
So it's going to be head.
And I'm going to give a value and say I'm going it pass.
So what you have to do now everyone you have to make sure that you complete this.
Okay.
It's going to be a lot easier I believe now we have the base case which we all know head is none.
We have the work which we will do.
So it is almost very similar to that first index of a number.
Also the problem which we did in recursion.
Right.
We used to first check And then we used to make the recursion call.
So almost all the things are same.
We can use another parameter index as well.
I think with all this knowledge, you should be pretty nicely be able to write the code for this, right?
And please just make sure that you are able to check on all these boundary cases in any in any online
assessment or say, uh, some websites like LeetCode, your code will fail for those boundary cases,
so you will get to know about that.
But in an interview you are in a way required to find out them.
Okay, so please have always the habit to think on the boundary cases while writing your code, not
by running and checking them directly online, because then we have not thought of it, right?
So that thought process, the one which is required to actually crack the interviews to be a good coder
that will not get built up tomorrow when we will make some softwares there also these kind of boundary
cases occur which we are able to then better think of.
Okay everyone.
So I hope that is clear.
Now let's meet in the next video.
Thank you.