Guest User

Untitled

a guest
Feb 16th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  1. class LinkedListNode
  2. attr_accessor :value, :next_node
  3.  
  4. def initialize(value, next_node=nil)
  5. @value = value
  6. @next_node = next_node
  7. end
  8. end
  9.  
  10. def reverse_list(list, previous=nil)
  11. if list
  12. next_node = list.next_node
  13. list.next_node = previous
  14. reverse_list(next_node, list)
  15. end
  16. end
  17.  
  18.  
  19. def print_values(list_node)
  20. if list_node
  21. print "#{list_node.value} --> "
  22. print_values(list_node.next_node)
  23. else
  24. print "nil\n"
  25. return
  26. end
  27. end
  28.  
  29. node1 = LinkedListNode.new(37)
  30. node2 = LinkedListNode.new(99, node1)
  31. node3 = LinkedListNode.new(12, node2)
  32.  
  33. print_values(node3)
  34.  
  35. puts "-------"
  36.  
  37. reverse_list(node3)
  38.  
  39. print_values(node1)
Add Comment
Please, Sign In to add comment