Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Elizabeth Deutsch - linked list methods #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

edeutschie
Copy link

Linked Lists

Congratulations! You're submitting your assignment.

Comprehension Questions

What is the time and space complexity for each method you implemented? Provide justification.

Question Answer
What is the time complexity of the insert method? Provide justification. O(1) because we only add one thing at the beginning.
What is the space complexity of the insert method? Provide justification. O(1) because the space complexity is constant (stores variables for @Head and current) and doesn't change no matter the length of the linked list.
|

| 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. |

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant