Elizabeth Deutsch - linked list methods #18
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Linked Lists
Congratulations! You're submitting your assignment.
Comprehension Questions
What is the time and space complexity for each method you implemented? Provide justification.
| What is the time complexity of the search method? Provide justification. | O(n) - because worst case we have to traverse the entire linked list to see if it contains a value. |
| What is the space complexity of the search method? Provide justification. | O(1) - because the space complexity is constant (stores variables for current, value, and @Head) and doesn't change no matter the length of the linked list. |
| What is the time complexity of the find_max method? Provide justification. | O(n) - because we have to traverse the entire linked list to compare values and see which is the max. |
| What is the space complexity of the find_max method? Provide justification. | O(1) - because the space complexity is constant (stores variables for current, max, and @Head) and doesn't change no matter the length of the linked list. |
| What is the time complexity of the find_min method? Provide justification. | O(n) - because we have to traverse the entire linked list to compare values and see which is the min. |
| What is the space complexity of the find_min method? Provide justification. | O(1) - because the space complexity is constant (stores variables for current, min, and @Head) and doesn't change no matter the length of the linked list. |
| What is the time complexity of the length method? Provide justification. | O(n) - because we have to traverse the entire linked list to calculate the length. |
| What is the space complexity of the length method? Provide justification. | O(1) - because the space complexity is constant (stores variables for current, count, and @Head) and doesn't change no matter the length of the linked list. |
| What is the time complexity of the find_nth_from_beginning method? Provide justification. | O(n) - because in the worst case if n is the length of the list, we have to traverse the entire linked list to find the value of the node that is nth from the beginning. |
| What is the space complexity of the find_nth_from_beginning method? Provide justification. | O(1) - because the space complexity is constant (stores variables for current, count, n, and @Head) and doesn't change no matter the length of the linked list. |
| What is the time complexity of the insert_ascending method? Provide justification. | O(n) - because in the worst case if the value is greater than any other nodes in the list, we have to traverse the entire linked list to add it to the end. |
| What is the space complexity of the insert_ascending method? Provide justification. | O(1) - because the space complexity is constant (stores variables for current, @Head, value) and doesn't change no matter the length of the linked list. |
| What is the time complexity of the visit method? Provide justification. | O(n) - because we have to visit each node in the linked list in order to print the value. |
| What is the space complexity of the visit method? Provide justification. | O(1) - because the space complexity is constant (stores variables for current, @Head) and doesn't change no matter the length of the linked list. |
| What is the time complexity of the delete method? Provide justification. | O(n) - because in the worst case if the value to delete is at the end of the list, we have to traverse the entire linked list to delete it. |
| What is the space complexity of the delete method? Provide justification. | O(1) - because the space complexity is constant (stores variables for current, @Head, value) and doesn't change no matter the length of the linked list. |
| What is the time complexity of the reverse method? Provide justification. | O(n) - because we have to traverse the entire linked list to reverse it. |
| What is the space complexity of the reverse method? Provide justification. | O(1) - because the space complexity is constant (stores variables for current, @Head, previous, temp) and doesn't change no matter the length of the linked list. |
| What is the time complexity of the find_middle_value method? Provide justification. | O(n) because we go through the list 1.5 times. But ultimately, only go through the one loop one time. |
| What is the space complexity of the find_middle_value method? Provide justification. | O(1) - because the space complexity is constant (stores variables for slow, fast, @Head) and doesn't change no matter the length of the linked list. We are not creating anything, size we are allocating doesn't change.
|
| What is the time complexity of the find_nth_from_end method? Provide justification. | O(n) - because we have to traverse through the entire the linked list to get to the end, which allows us to know which node is the nth from the end. |
| What is the space complexity of the find_nth_from_end method? Provide justification. | O(1) - because the space complexity is constant (stores variables for current, trailingCurrent, @Head) and doesn't change no matter the length of the linked list. |
| What is the time complexity of the has_cycle method? Provide justification. | O(n) because we traverse the list either to the end if it does not have a cycle or fast catches up with slow. But in the second case, it's still a factor of n, so O(n). |
| What is the space complexity of the has_cycle method? Provide justification. | O(1) - because the space complexity is constant (stores variables for slow, fast, @Head) and doesn't change no matter the length of the linked list. |