Hi everyone.
Let's do this question now.
This question is about designing a singly linked list.
So let's read it.
Design a singly linked list class that has a head and tail.
Every node is to have two attributes value the value of the current node, and next a pointer to the
next node.
The linked list is to be zero indexed.
The class should support the following.
So that's the question.
Now the class should support the following.
The first is we have singly linked list over here.
Now this initializes the singly linked list object.
So we have seen previously that a class is a blueprint for creating objects.
So it's defining a pattern.
And objects can have uh parameters and methods.
So we have seen that before.
All right.
So again we will just have a class and its name will be singly linked list.
And then we can just say uh const um singly linked list one is equal to new singly linked list.
So that's how we will create this object over here.
All right.
So this is going to be pretty straightforward.
And we have seen this before.
And we will again visit this when we code out our solution.
Now let's read the remaining part over here.
It's mentioned that some instance methods should be written for this particular class.
So the first instance method is get index.
Now over here the index is passed to it.
Now this should get the value of the index node.
If the index is invalid return minus one.
So for example if our singly linked list is something like this.
So we have one pointing to two.
And then we have this pointing to a node with value three.
And this points to null.
So this is a singly linked list.
Now if we pass index one then it should get this particular node over here.
Again this is index zero because it's mentioned that the linked list is to be zero indexed.
So this is zero.
This is one and this is two.
And again remember we cannot do this in constant time as in the case of an array because the indices
are not stored um in inbuilt manner.
Right.
So we'll have to traverse and we'll have to use a count variable for that.
All right.
Now let's continue reading the question.
The next instance method is add add head and a value is passed to it.
So a value would be passed.
And we have to add a node of the given value before the first element of the linked list.
So for example, if the value four is passed and added head instance method is called for this singly
linked list, then we would have to make a node with value four and that should be added at the head.
So that's the second instance method over here.
Now the next one is add a tail and a value is passed.
So again in a similar manner if we want to add five.
So we would have to add it over here.
So this three will be pointing to five which will point to null.
So add a node of given value at the last element of the linked list.
Now the next instance method is added index and index and value is passed to add a node of given value
before the index node in the linked list.
If the index equals the length of the linked list, the node will be appended to the end of the linked
list.
If index is greater than the length, don't insert the node.
So this is very clear.
So for example in this linked list over here let's write the indices zero, one two and three.
So if the index is equal to four over here when we call this function then we will add it at the end
as the tail.
So it's mentioned over here if index equals the length.
So the length over here is four.
The node will be appended to the end of the linked list.
If index is greater than the length for example over here the length is four.
And index let's say is 5 or 10.
In that case we won't insert the node.
So that's what is mentioned over here.
And then if we want to insert a node at index one, it's going to be inserted before this.
So that the new node let's say that's value that has value five.
The new node.
Once it is inserted over here it will be at index one.
So that is what is mentioned over here.
Add a node of given value before the index node in the linked list.
So that is added index function or method over here.
And then the last instance method is delete at index.
And we only pass the index.
So delete the index node in the linked list if the index is valid.
Else nothing happens.
So for example, if this is the linked list and we want to delete this particular node, then this will
be removed.
And this will point to the next node.
So these are the instance methods that we have to write.
So let's go ahead and see in the next video how we can solve this question.