Conversation
| def find_max | ||
| puts "Not implemented" | ||
| current = @head | ||
| max = current.data |
There was a problem hiding this comment.
Check to ensure that head is not nil before accessing data. In other words, update your method to work for empty linked list scenario.
| # returns the data value and not the node | ||
| def find_min | ||
| current = @head | ||
| min = current.data |
There was a problem hiding this comment.
Same as with find_max, check for null before accessing member variables in the node.
| def length | ||
| counter = 1 | ||
| current = @head | ||
| while current.next |
There was a problem hiding this comment.
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.
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.
| current = current.next | ||
| prevVal = nil | ||
| # nextVal = nil | ||
| until value < current.data |
There was a problem hiding this comment.
check if current is nil (or @Head is nil) before accessing current.data
|
|
||
| new_node = Node.new(value) | ||
|
|
||
| if value > current.data && !current.next |
There was a problem hiding this comment.
Check if current is nil first. :-)
There was a problem hiding this comment.
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.
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.
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.
| # 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" |
| puts "THIS MAKES MY HEAD HURT" | ||
| current = @head | ||
| previous = nil | ||
| temp = current.next |
There was a problem hiding this comment.
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.