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

Erica Case - Linked-List #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

EricaJCasePhD
Copy link

Restricted Array

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): only adding to the beginning there is no need to run through the list.
What is the space complexity of the insert method? Provide justification. O(1) only need to store one node temporarily regardless of size.
What is the time complexity of the search method? Provide justification. O(n). Need to go through linked list once to check all values.
What is the space complexity of the search method? Provide justification. O(1): no additional storage.
What is the time complexity of the find_max method? Provide justification. O(n): need to go through list once to find max value.
What is the space complexity of the find_max method? Provide justification. O(1) only one additional integer stored regardless of list size.
What is the time complexity of the find_min method? Provide justification. O (n): need to go through list once to find max value.
What is the space complexity of the find_min method? Provide justification. O(1) only one additional integer stored regardless of list size.
What is the time complexity of the length method? Provide justification. O(n): only need to go through list to get number of elements.
What is the space complexity of the length method? Provide justification. O(1): additional storage is one integer (a counter)
What is the time complexity of the find_nth_from_beginning method? Provide justification. O(n): will have to traverse the entire list if n>= length(list)
What is the space complexity of the find_nth_from_beginning method? Provide justification. O(1): only storage is additional counters.
What is the time complexity of the insert_ascending method? Provide justification. O(n): have to traverse list (average: n/2 or worst n)
What is the space complexity of the insert_ascending method? Provide justification. O(1) only store one node at a time in additional storage.
What is the time complexity of the visit method? Provide justification. O(n)...need to traverse the length of the list.
What is the space complexity of the visit method? Provide justification. O(1): additional storage is one node.
What is the time complexity of the delete method? Provide justification. O(n): traverse the list to value
What is the space complexity of the delete method? Provide justification. O(1): only store one node at a time.
What is the time complexity of the reverse method? Provide justification. O(n): have to traverse the linked list.
What is the space complexity of the reverse method? Provide justification. O(1)..technically four nodes are stored, but this translates to O(1)
What is the time complexity of the find_middle_value method? Provide justification. O(n): goes through array once
What is the space complexity of the find_middle_value method? Provide justification. O(1): additional storage is a node and a single value
What is the time complexity of the find_nth_from_end method? Provide justification. O(n): walk through the loop once
What is the space complexity of the find_nth_from_end method? Provide justification. O(1) only 2 nodes stored regardless of size.
What is the time complexity of the has_cycle method? Provide justification. O(1): catches after going through once cycle
What is the space complexity of the has_cycle method? Provide justification. O(1): only store two nodes at a time regardless of list size.

linked_list.py Outdated
# method to find if the linked list contains a node with specified value
# returns true if found, false otherwise
def search(value):
pass
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Send me a slack DM, when you complete this. So I know to review then.

linked_list.py Outdated

node = self.__head
max = node.get_data()
while node.next_node != None:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be node != None: instead of node.next_node != None: ? Check if you get the right result when the last node in the linked list has the max value.

mid_val = node.get_data()

while node.next_node and node.next_node.next_node:
mid_val = node.next_node.get_data()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's say the linked list is 1 -> 2 -> 3 -> 4 -> 5. I expect to get back 3, but this method will return 4. Think more on what the algorithm should look like.

# # checks if the linked list has a cycle. A cycle exists if any node in the
# # linked list links to a node already visited.
# # returns true if a cycle is found, false otherwise.
def has_cycle(self):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The time complexity for has_cycle will be O(n) and not O(1) - since it will depend on the length of the list.

return False
slow_loop = self.__head
fast_loop = self.__head.next_node
while fast_loop.next_node.next_node:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if fast_loop.next_node is None:? null dereference issue. Check for fast_loop.next_node before checking for fast_loop.next_node.next_node.

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.

2 participants