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

Aurora Anderson Linked List #17

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

Conversation

auroralemieux
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. constant -- no need to visit all, just head.
What is the space complexity of the insert method? Provide justification. constant -- no data structures, just the new node.
What is the time complexity of the search method? Provide justification. O(n) -- at worst visiting all nodes in linked list.
What is the space complexity of the search method? Provide justification. constant -- no new data structures needed.
What is the time complexity of the find_max method? Provide justification. O(n) - visiting all at worst.
What is the space complexity of the find_max method? Provide justification. constant -- no new structures
What is the time complexity of the find_min method? Provide justification. O(n) -- visiting all at worst
What is the space complexity of the find_min method? Provide justification. constant -- no new structures
What is the time complexity of the length method? Provide justification. O(n) -- counting all nodes.
What is the space complexity of the length method? Provide justification. no data structures, just a variable, so constant
What is the time complexity of the find_nth_from_beginning method? Provide justification. O(n) as potentially visiting all nodes
What is the space complexity of the find_nth_from_beginning method? Provide justification. constant -- no new data structures
What is the time complexity of the insert_ascending method? Provide justification. O(n) in case of inserting at end
What is the space complexity of the insert_ascending method? Provide justification. constant as only variables needed
What is the time complexity of the visit method? Provide justification. O(n) printing each node
What is the space complexity of the visit method? Provide justification. constant -- no new data structures needed
What is the time complexity of the delete method? Provide justification. O(n) as potentially visiting all before finding target value
What is the space complexity of the delete method? Provide justification. constant -- no new data structures
What is the time complexity of the reverse method? Provide justification. O(n) as visiting each node
What is the space complexity of the reverse method? Provide justification. constant even though multiple variables, as they are reassigned and no complex data structures needed
What is the time complexity of the find_middle_value method? Provide justification. this is O(n) although technically worst case 2(O(n)) -- but constants don't matter
What is the space complexity of the find_middle_value method? Provide justification. constant because no complex data structures
What is the time complexity of the find_nth_from_end method? Provide justification.
What is the space complexity of the find_nth_from_end method? Provide justification.
What is the time complexity of the has_cycle method? Provide justification.
What is the space complexity of the has_cycle method? Provide justification.

@@ -42,17 +42,41 @@ def search(value)
# 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.data
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 head is not nil before accessing data. In other words, update your method to work for empty linked list scenario.

linked_list.rb Outdated
end

# method to return the min value in the linked list
# returns the data value and not the node
def find_min
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 as with find_max, check for null before accessing member variables in the node.

linked_list.rb Outdated
puts "Not implemented"
end

# method that returns the length of the singly linked list
def length
counter = 1
current = @head
while 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.

Always check for null. :-) What if the linked list is empty? In that case, the length returned should be 0.

index = 0
current = @head
if current
while index < n
Copy link
Collaborator

Choose a reason for hiding this comment

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

What if there are less than n+1 number of nodes in the list?
To fix for that scenario, Reverse the order of if and while statements and if current become null sooner than the n, return null.

linked_list.rb Outdated
current = current.next
prevVal = nil
# nextVal = nil
until value < 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.

check if current is nil (or @Head is nil) before accessing current.data

linked_list.rb Outdated

new_node = Node.new(value)

if value > current.data && !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 if current is nil first. :-)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Scenarios to ensure your code works for: empty linked list, linked list has all values smaller than the value to insert, linked list with all values greater than the value to insert. In the empty linked list case and all values greater than value to insert case, head will need to get updated.

end
while current.data != value
prevNode = current
current.next ? current = current.next : break
Copy link
Collaborator

@shrutivanw shrutivanw Oct 31, 2017

Choose a reason for hiding this comment

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

Should this be current.next ? current = current.next : return If you just break, you'll have a nil dereference exception on line 142.

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

Choose a reason for hiding this comment

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

Optimization: you could replace lines 143 through 147 with just line 144, i.e. prevNode.next = current.next. If current.next is nil, line 144 will just set prevNode.next to nil.

end

# method to reverse the singly linked list
# note: the nodes should be moved and not just the values in the nodes
def reverse
puts "Not implemented"
puts "THIS MAKES MY HEAD HURT"
Copy link
Collaborator

Choose a reason for hiding this comment

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

:-)

puts "THIS MAKES MY HEAD HURT"
current = @head
previous = nil
temp = current.next
Copy link
Collaborator

@shrutivanw shrutivanw Oct 31, 2017

Choose a reason for hiding this comment

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

Check for empty linked list case. Don't access current.next without verifying that current exists (and is not nil).

@shrutivanw
Copy link
Collaborator

Hi Aurora, see comments inline.

The common themes to watch out while working with linked lists are:

(i) always check and make sure that the node is not nil before accessing member variables (data, next or previous)

(ii) check if your method will work if

  • the linked list is empty (aka head is nil),
  • the linked list has less number of nodes than what you expect,
  • the linked list nodes have values that are all greater than a certain value,
  • the linked list nodes have values that are all less than a certain value,

    (ii) and in the case of doubly linked list, don't forget to update previous along with next.

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