Last active
September 11, 2015 23:59
-
-
Save dsshap/4afb9e4eaff2ed0949c5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def acquire_values_recursively(list_node, val_array=[]) | |
return [] if list_node.nil? | |
val_array << list_node.value | |
unless list_node.next_node.nil? | |
acquire_values_recursively(list_node.next_node, val_array) | |
end | |
return val_array | |
end | |
- compared to - | |
def acquire_values_recursively(list_node, val_array=[]) | |
return [] if list_node.nil? | |
if list_node.next_node.nil? | |
val_array << list_node.value | |
return val_array | |
else | |
val_array << list_node.value | |
acquire_values_recursively(list_node.next_node, val_array) | |
end | |
return val_array | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice, I like the optimization. Thanks!
The only reason I kept it in there is because it throws a stack error if there is an infinite list to match a test. I should have thrown the test out, but sometimes bubbling a payload up the stack with desired ordering is tricky and nice to demonstrate.