Welcome back, dear reader. So, how is it going? We hope you have got a basic idea of the linked list data structure. So, let's discuss this problem.
Important Links :Question Video, Solution Video
Welcome back, dear reader. So, how is it going? We hope you have got a basic idea of the linked list data structure. So, let's discuss this problem.
Important Links :Question Video, Solution Video
We have to display a linked list and we also have to complete the size function of the linked list. You may refer to the question video if you have any doubts regarding the question. So, let's get started.
Consider the linked list shown below:
The linked list has its head at address 5k and its tail at address 9k. (These are random addresses just to depict that each node has an address.) Now, we need to write the size() function and the display() function for the linked list. We recommend you try to solve this question yourself and then move on to the solution.
Size: Well if you notice, the linked list class that we have created already has a size data member in it. Also, when we add an element to the end of the linked list using the addLast() function, we increment the size value. So, the size value is maintained throughout the creation of the linked list as well as during the insertion of the nodes at the end of the list. So, the size data member already contains the size of the linked list. So, we just have to return it.
So, hope that this is clear to you. Now, let us move forward with the display function.
Display: The first question is how we want to display the elements of the linked list. Well, we want space-separated elements to be displayed in a single line. Now, look at the diagram given below:
To display the linked list we take a temporary Node type of variable "t" at the head of the linked list. In the call stack, we have shown the memory allocated to the linked list summary object. After that the variable t stores the address of the head node i.e. 5k. Now let us discuss the procedure:
The above-mentioned steps are applied and the traversal of the linked list is shown below:
The linked list is displayed by applying the above-mentioned procedure step by step as shown in the above diagrams. When t is at the head it has a value of the address of the head i.e. 5k. So, it prints t.data which is the data of the node at which t resides and that is 10. After this, we move t to its next by doing t=t.next. So, the value of t changes from 5k to 6k as shown in the figure. So, the data at address 6k i.e. 20 gets printed. We follow the same procedure and finally, we reach the last element i.e. t=9k. We print the data and move to t.next i.e. t becomes null. This is where we have to stop and our linked list is completely displayed.
You may refer to the solution video to understand the above procedure if you have any doubts about it. So, now that we know the procedure, let's write the code.
java; true";
Now, let us discuss the time and space complexity of the above methods.
The space complexity of the above methods is O(1) as we have not used any extra space for any of the above methods.
So, dear reader, we hope that you understood the above methods and the code completely and also understood the time and space complexity analysis. If you still have any doubts, you may refer to the complete solution video to clear them.
Here are some suggestions from our side that you don't want to miss: