Skip to content

Instantly share code, notes, and snippets.

@msarit
Last active April 6, 2018 20:28
Show Gist options
  • Save msarit/05f3e8c738f71b609224beeabb67fa86 to your computer and use it in GitHub Desktop.
Save msarit/05f3e8c738f71b609224beeabb67fa86 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, @data)
end
def pop
return nil if @data.nil?
popped_value = @data.value
@data = @data.next_node
return popped_value
end
end
def reverse_list(list)
stack = Stack.new
while list
stack.push(list.value)
list = list.next_node
end
return stack.data
end
def mutate_list(list, previous=nil)
while list
node = LinkedListNode.new(list.value, previous)
previous = node
list = list.next_node
end
return node
end
def print_values(list_node)
if list_node
print "#{list_node.value} --> "
print_values(list_node.next_node)
else
print "nil\n"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment