Created
January 2, 2014 20:46
-
-
Save rottmanj/8226513 to your computer and use it in GitHub Desktop.
Ruby Linked List Implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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