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

Jou-Jou's Linked Lists #5

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

Jou-Jou's Linked Lists #5

wants to merge 1 commit into from

Conversation

jjousun
Copy link

@jjousun jjousun commented Aug 27, 2017

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). Since we're adding to the beginning, it takes constant time to find the beginning and to insert
What is the space complexity of the insert method? Provide justification. O(1). The new node created doesn't vary depending on input size (since it's always one at a time).
What is the time complexity of the search method? Provide justification. O(n). Must visit every node once.
What is the space complexity of the search method? Provide justification. O(1). A pointer (or reference) is created to move along the linked list, but this doesn't change.
What is the time complexity of the find_max method? Provide justification. O(n). Must visit every node once.
What is the space complexity of the find_max method? Provide justification. O(1). A pointer (or reference) is created to move along the linked list, but this doesn't change.
What is the time complexity of the find_min method? Provide justification. O(n). Must visit every node once.
What is the space complexity of the find_min method? Provide justification. O(1). A pointer (or reference) is created to move along the linked list, but this doesn't change.
What is the time complexity of the length method? Provide justification. O(n). Must visit every node once.
What is the space complexity of the length method? Provide justification. O(1). Additional storage doesn't depend on input size.
What is the time complexity of the find_nth_from_beginning method? Provide justification. O(n). Must visit every node (in worst case).
What is the space complexity of the find_nth_from_beginning method? Provide justification. O(1). Additional storage doesn't depend on input size.
What is the time complexity of the insert_ascending method? Provide justification. O(n). Must visit every node to find the place to insert. O(1) to insert, so O(n) overall.
What is the space complexity of the insert_ascending method? Provide justification. O(1). Doesn't depend on input size.
What is the time complexity of the visit method? Provide justification. O(n). Must visit every node.
What is the space complexity of the visit method? Provide justification. O(1). Doesn't depend on input size.
What is the time complexity of the delete method? Provide justification. O(n). O(n) to find the node to delete (visiting, in worst case, every node), and the O(1) to delete. So O(n) overall.
What is the space complexity of the delete method? Provide justification. O(1). Doesn't change if size of linked list changes.
What is the time complexity of the reverse method? Provide justification. O(n). Visit every node once.
What is the space complexity of the reverse method? Provide justification. O(1). Additional storage doesn't change if size of linked list changes.
What is the time complexity of the find_middle_value method? Provide justification. O(n). Must visit every node once.
What is the space complexity of the find_middle_value method? Provide justification. O(1). Additional storage doesn't depend on size of linked list.
What is the time complexity of the find_nth_from_end method? Provide justification. O(n). Must go through the entire linked list.
What is the space complexity of the find_nth_from_end method? Provide justification. O(1). Additional storage remains constant even if linked list size changes.
What is the time complexity of the has_cycle method? Provide justification. O(n). Even if there is a cycle, must go through the entire linked list (although technically it might be a little longer than O(n) in order to catch the cycle, I think it still comes out to linear time). i.e. mathematically it might be like O(n + n/2 + n/4 + n/8....)
What is the space complexity of the has_cycle method? Provide justification. O(1). Additional storage is constant despite input size.

end

# method to return the max value in the linked list
# returns the data value and not the node
def find_max
puts "Not implemented"
current = @head
max = current
Copy link
Collaborator

Choose a reason for hiding this comment

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

What is head is nil? You won't be able to return max.data in that case. Always check for nil before dereferencing.

end

# method to return the min value in the linked list
# returns the data value and not the node
def find_min
puts "Not implemented"
current = @head
min = current.data # not sure why in the previous menthod couldn't use .data here
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same comment as above. Check if current is nil before accessing current.data.

i = 0

until i == n
current = current.next
Copy link
Collaborator

Choose a reason for hiding this comment

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

Check for nil before moving to next. Check for the case where the linked list has less than n nodes.

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