Skip to content

Instantly share code, notes, and snippets.

@ashlynnpai
Created August 28, 2016 01:44
Show Gist options
  • Save ashlynnpai/1345f2c18bcb6e04e48ba20608958f73 to your computer and use it in GitHub Desktop.
Save ashlynnpai/1345f2c18bcb6e04e48ba20608958f73 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
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
old_next_node = list.next_node
list.next_node = previous
previous = list
list = old_next_node
end
previous
previous.print_values(previous)
end
end
node1 = LinkedListNode.new(37)
node2 = LinkedListNode.new(99, node1)
node3 = LinkedListNode.new(12, node2)
node3.print_values(node3)
node3.reverse_list(node3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment