If you have read the above mentioned points regarding what you are required to do and understand them then trust me if we will just follow these steps as it is and we will be done with our problem. Let's talk about each function in detail.
getFirst
Time Complexity: O(1)
Space Complexity: O(1)
What does this function say? It says that it should return the data of the first element. If empty should return -1 and print "List is empty".
We literally just need to tackle these two points.
- return the data of first element:
The first element of Linked List can be accessed using keyword head.data, where head points towards the first element's address and .data helps to access the value at the address.
- If empty should return -1 and print "List is empty":
It is possible that the given Linked List is empty which implies that the size of the Linked List is 0. So before simply accessing the head of Linked List, it is important that you check this using an "if" condition, whether the size is 0 or not.
If it is 0, then you need to print "List is empty" and return -1 as mentioned in the question.
Else you can return head.data.
getLast
Time Complexity: O(1)
Space Complexity: O(1)
What does this function say? It says that it should return the data of the last element. If empty should return -1 and print "List is empty".
Doesn't this statement look similar to the previous one?
Yes, it's similar. The only difference is "last" instead of "first". Let's look at it.
We literally just need to tackle these two points like before.
- return the data of last element:
The last element of Linked List can be accessed using keyword tail.data, where tail points towards the last element's address and .data helps to access the value at the address.
- If empty should return -1 and print "List is empty":
It is possible that the given Linked List is empty which implies that the size of Linked List is 0. So before simply accessing the head of Linked List, it is important that you check this using an "if" condition, whether the size is 0 or not.
If it is 0, then you need to print "List is empty" and return -1 as mentioned in the question.
Else you can return tail.data.
getAt
Time Complexity: O(n)
Space Complexity: O(1)
What does this function say? It says that it should return the data of elements available at the index passed. If empty should return -1 and print "List is empty". If invalid index is passed, it should return -1 and print "Invalid arguments".
Let's handle these conditions one by one.
- return the data of element available at the index passed:
To access the data of elements present at index passed, we need to define a node, temp with address of head then apply a for loop, initializing with 0 to one less than given index.
Then we update temp with temp.next which at i = 0, points towards the address of element at index 1.
That's the reason we set the limit of for loop till one less than the index. So that for index = 5, loop runs till i = 4 but temp points towards the address of the 5th index element.
After exiting the for loop, our temp points toward the address of the element, of which we need to return data.
So at this point we simply return temp.data.
- If empty should return -1 and print "List is empty":
It is possible that the given Linked List is empty which implies that the size of Linked List is 0. So before simply accessing the head of Linked List, it is important that you check this using an 'if' condition, whether the size is 0 or not.
If it is 0, then you need to print "List is empty" and return -1 as mentioned in the question.
Else you can return the data at a given index with the help of steps discussed above.
- If invalid index is passed, should return -1 and print "Invalid arguments":
It is also possible that the index which the user passes is an invalid index. An invalid index is defined if the index is not in the range of 0 to size -1 that means either the index is smaller than 0 or greater than or equal to size.
Before taking the control to for loop it is necessary that you check this using an "if" condition.
If the index is either smaller than 0 or greater than or equal to size then we print "" and return -1.
Yes! We are done with all the three different functions that return us value according to their use.