Skip to content

Instantly share code, notes, and snippets.

@kenmazaika
Created April 11, 2015 00:04
Show Gist options
  • Save kenmazaika/69b3a3ee20cb3b0b8336 to your computer and use it in GitHub Desktop.
Save kenmazaika/69b3a3ee20cb3b0b8336 to your computer and use it in GitHub Desktop.
class Node
attr_accessor :value, :next_node
def initialize(value, next_node)
@value = value
@next_node = next_node
end
def print_values
print "#{self.value}-->"
if self.next_node.nil?
print "nil\n"
return
else
self.next_node.print_values
end
end
end
def reverse_list(list)
rev = nil
while list
rev = Node.new(list.value, rev)
list = list.next_node
end
rev
end
l1 = Node.new(1, nil)
l2 = Node.new(2, l1)
l3 = Node.new(3, l2)
l4 = Node.new(4, l3)
l4.print_values
rev = reverse_list(l4)
puts "-----"
rev.print_values
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment