![]() ![]() That is because by that time this variable inside the function is referencing to the global or the window object, basically what we have done here is we have polluted the global object. It seems like all the properties we passed to the function without the new keyword has been set to the window object. Simply put there has no effect on the output whether we use new keyword or not with the factory functions.Ĭhanges are reflected on the window object. That is because since we are not using this keyword inside the function and we are explicitly returning a custom object other than the this, there will be no use of having the implicit code. function person(firstName, lastName, age) Įven though implicit code added to the factory function when invoking with the new keyword, there will be a zero effect on the outcome. When it does so without the new keyword, it’s a factory function. In JavaScript, any function can return an object. The time complexity of both approaches is O(N) where N is the length of the linked list while the space complexity of the recursive approach is O(N) due to stack size.A factory function is any function which is not a class or constructor that returns a (presumably new) object. We have used the while loop and the class concepts in this article in the first approach and the recursive approach to finding the length in the second approach. In this tutorial, we have learned how to find the length of a given linked list by implementing the code and going through the edge cases. The space complexity of the above code is O(N) because there are total N calls and for each call, we have to maintain the current node stack. The time complexity of the recursive approach is O(N), where N is the number of nodes present in the given linked list. Let’s see the code − // creating the linked list node As a return value, we will return 1 + recursive call return value and the result will be given at the first call. In this method, we are going to call the function with the head of the linked list, and then from the function, we will again call the function but with the parameter next node of the current node. ExampleĬalling the same function with a different parameter from the function itself with a particular base condition is known as recursion. In this method, we are going to follow the same steps as we have gone through in the above method for creating the linked list, for the main task we will use the recursive approach. So, the time complexity of the above-given method is O(N) where N is the size of the linked list and the space complexity of the above method is O(1). In the above-given method, we are not using any extra space and traversing over the linked list only once. creating the linked list nodeĬonsole.log("Length of the given linked list is: " + length(head)) In the process, we will use the while loop, the head or the starting node to the linked list, and a variable to count the number of nodes in the linked list, that is the length of the given linked list. We will define some functions like the push function to add the values to the linked list by simply passing the head and the data. In this method, we are going to create a linked list first using the class to provide the structure to the linked list. We are going to use two approaches first the direct iterative approach using the while loop and another the recursive approach to find the length of the given linked list. As the length of the linked list can be changed, so we will find the length of the linked list at a particular point of code. In the given problem we are given a linked list and first, we have to create the linked list using the class, then after we have to find the length of the given linked list. We will use the while loop and the class concepts in this article. In this article, we are going to find the length of a given linked list by implementing the code and going through the edge cases. A linked list is a linear data structure that can be a variable length and the length of the linked list can be changed, which was a problem in the array that the length of the array cannot be changed.
0 Comments
Leave a Reply. |