Skip to content

Instantly share code, notes, and snippets.

@kenmazaika
Last active October 4, 2018 19:07
Show Gist options
  • Save kenmazaika/dc422b7ad7357e98f2325195424f7f04 to your computer and use it in GitHub Desktop.
Save kenmazaika/dc422b7ad7357e98f2325195424f7f04 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
class Stack
attr_reader :data
def initialize
@data = nil
end
def push(value)
@data = LinkedListNode.new(value)
end
def pop
node_value = @data.value
@data = @data.next_node
return node_value
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 reverse_list(list, previous=nil)
while list != nil
old_list = list.next_node
list.next_node = previous
previous = list
list = old_list
end
return previous
end
n1 = LinkedListNode.new(37)
n2 = LinkedListNode.new(99, n1)
n3 = LinkedListNode.new(12, n2)
print_values(n3)
reverse = reverse_list(n3)
print_values(reverse)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment