Skip to content

Instantly share code, notes, and snippets.

@gagaception
Created October 13, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gagaception/8649127d866159d98dfa to your computer and use it in GitHub Desktop.
Save gagaception/8649127d866159d98dfa to your computer and use it in GitHub Desktop.
class LinkedListNode
attr_accessor :value, :next_node
def initialize(value, next_node=nil)
@value = value
@next_node = next_node
end
end
def print_values(list_node)
print "#{list_node.value} --> "
if list_node.next_node.nil?
print "nil\n"
return
else
print_values(list_node.next_node)
end
end
class Stack
attr_reader :data
def initialize
@data = nil
end
def push(value)
@data = LinkedListNode.new(value, @data)
end
def pop
result = @data.value
@data = @data.next_node
return result
end
end
def reverse_list(list)
stack = Stack.new()
while list
stack.push(list.value)
list = list.next_node
end
return stack.data
end
node1 = LinkedListNode.new(37)
node2 = LinkedListNode.new(99, node1)
node3 = LinkedListNode.new(12, node2)
print_values(node3)
puts "-------"
revlist = reverse_list(node3)
print_values(revlist)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment