-
Notifications
You must be signed in to change notification settings - Fork 28
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
base: master
Are you sure you want to change the base?
Conversation
@@ -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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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. :-)
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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" |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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).
Hi Aurora, see comments inline.
|
Linked Lists
Congratulations! You're submitting your assignment.
Comprehension Questions
What is the time and space complexity for each method you implemented? Provide justification.