Skip to content

Instantly share code, notes, and snippets.

@rottmanj
Created January 2, 2014 20:46
Show Gist options
  • Save rottmanj/8226513 to your computer and use it in GitHub Desktop.
Save rottmanj/8226513 to your computer and use it in GitHub Desktop.
Ruby Linked List Implementation
class Node
attr_accessor :node, :next
def self.last(node)
return node if node.next.nil?
node = last node.next
end
def self.reverse(node)
return node if node.next.nil?
head, swap, node.next = node.next, node, nil
link = head.next
while link != nil
head.next = swap
swap = head
head = link
link = link.next
end
head.next = swap
head
end
def self.node_list(node, msg = nil)
msg ||= ""
return msg[0..-4] if node.nil?
node_list(node.next, msg << "#{node.node} -> ")
end
def initialize(node)
@node = node
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment