Skip to content

Instantly share code, notes, and snippets.

@isomorpheric
Created March 5, 2016 23:29
Show Gist options
  • Save isomorpheric/6515c9e458e33468790f to your computer and use it in GitHub Desktop.
Save isomorpheric/6515c9e458e33468790f to your computer and use it in GitHub Desktop.
class Node
attr_accessor :value, :next_node
def initialize(value, next_node=nil)
@value = value
@next_node = next_node
end
end
def print_values(list_node)
if list_node
print "#{list_node.value} --> "
print_values(list_node.next_node)
else
print "nil\n"
return
end
end
def rev_list(list, previous=nil)
while list
reversed = Node.new(list.value, previous)
list = list.next_node
previous = reversed
end
return reversed
end
node1 = Node.new(37)
node2 = Node.new(99, node1)
node3 = Node.new(12, node2)
# Print original list
print_values(node3)
# Print reversed list
print_values(rev_list(node3))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment