daily pastebin goal
76%
SHARE
TWEET

Untitled

a guest Dec 12th, 2018 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class LinkedListNode
  2.  
  3. attr_accessor :value, :next_node
  4.  
  5.   def initialize(value, next_node=nil)
  6.     @value = value
  7.     @next_node = next_node
  8.   end
  9.  
  10. end
  11.  
  12.  
  13.  
  14. def reverse_list(list, previous=nil)
  15. x=0
  16.  
  17.   while list
  18.     temp = list.next_node
  19.     list.next_node = previous
  20.     previous = list
  21.  
  22.     list = temp
  23.  
  24.  
  25.     newhead = previous
  26.  
  27.     x = x + 1
  28.   end
  29.   return newhead
  30. end
  31.  
  32. def print_values(list_node)
  33.     if list_node
  34.       print "#{list_node.value} --> "
  35.       print_values(list_node.next_node)
  36.     else
  37.       print "nil\n"
  38.       return
  39.     end
  40. end
  41.  
  42.  
  43. node1 = LinkedListNode.new(37)
  44. node2 = LinkedListNode.new(99, node1)
  45. node3 = LinkedListNode.new(12, node2)
  46.  
  47.  
  48. print_values(node3)
  49.  
  50. puts "-------"
  51.  
  52. revlist = reverse_list(node3)
  53.  
  54. print_values(revlist)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top