Skip to content

Conversation

@ssamant
Copy link

@ssamant ssamant commented Aug 31, 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) -- don't need to visit any elements in the list
What is the space complexity of the insert method? Provide justification. O(1) -- don't need any additional storage
What is the time complexity of the search method? Provide justification. O(n) -- may need to visit entire list to find element
What is the space complexity of the search method? Provide justification. O(1) -- don't need any additional storage to search
What is the time complexity of the find_max method? Provide justification. O(n) -- will need to visit every element to make sure the max is found
What is the space complexity of the find_max method? Provide justification. O(1) -- need a temp storage variable but nothing more
What is the time complexity of the find_min method? Provide justification. O(n) -- will need to visit every element to make sure the min is found
What is the space complexity of the find_min method? Provide justification. O(1) -- need a temp storage var and that's it
What is the time complexity of the length method? Provide justification. O(n) -- need to visit each element to determine length
What is the space complexity of the length method? Provide justification. O(1) -- need a counter
What is the time complexity of the find_nth_from_beginning method? Provide justification. O(n) where n is the index not the length -- only need to search until you've found the nth element (worst case n = length of the list)
What is the space complexity of the find_nth_from_beginning method? Provide justification. O(1) -- no additional storage needed
What is the time complexity of the insert_ascending method? Provide justification. O(n) -- worst case you have to go to the end of the list
What is the space complexity of the insert_ascending method? Provide justification. O(1) -- no additional storage needed
What is the time complexity of the visit method? Provide justification. O(n) -- need to visit each element to print it
What is the space complexity of the visit method? Provide justification. O(1) -- no extra storage needed
What is the time complexity of the delete method? Provide justification. O(n) - worst case the item to delete is the last thing in the list
What is the space complexity of the delete method? Provide justification. O(1) need a temp var when you're resetting the pointers but that's it
What is the time complexity of the reverse method? Provide justification. O(n) -- have to visit all the elements in the list to reorder them (by changing the pointers)
What is the space complexity of the reverse method? Provide justification. O(1) need two temp vars to keep track of pointers
What is the time complexity of the find_middle_value method? Provide justification. O(n) one of the counters has to go to the end of the list and then you know the other counter is at the midpoint
What is the space complexity of the find_middle_value method? Provide justification. O(1) -- need two counters and no other storage
What is the time complexity of the find_nth_from_end method? Provide justification. O(n) -- two counters but the first one goes all they way to the end of the list
What is the space complexity of the find_nth_from_end method? Provide justification. O(1) -- two counters, no ADTs
What is the time complexity of the has_cycle method? Provide justification. worse than O(n) -- worst case the first counter has to cycle through before catching up to the second counter.
What is the space complexity of the has_cycle method? Provide justification. O(1) -- no ADTs needed

puts "Not implemented"

current = @head
max = current.data
Copy link
Collaborator

Choose a reason for hiding this comment

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

Always check if the node is nil before accessing data in it. This will give a nil dereference error is @Head is nil. So, check to ensure that @Head is not nil before accessing .data.

def find_min
puts "Not implemented"
current = @head
min = current.data
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 with find_max wrt nil dereference.

size = 0
until size == n do
size += 1
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.

This will have an issue if n is greater than number of nodes in the linked list. Always check to ensure that @Head is not nil. Then, in the loop, check to ensure that current is not nil before accessing current.next.

current = current.next
end
else
current = current.next
Copy link
Collaborator

@shrutivanw shrutivanw Oct 16, 2017

Choose a reason for hiding this comment

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

What happens when @Head is nil? What happens when @head.data > value?

current = @head

until i == n do
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 to ensure that current is not nil before doing a .next on it.

Copy link
Collaborator

Choose a reason for hiding this comment

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

In other words, make your code work for cases where number of nodes is less than n.

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