Skip to content

Instantly share code, notes, and snippets.

@epoch
Created March 20, 2014 00:48
Show Gist options
  • Select an option

  • Save epoch/9655025 to your computer and use it in GitHub Desktop.

Select an option

Save epoch/9655025 to your computer and use it in GitHub Desktop.
linked list template
class Node
attr_accessor :value, :next_node
def initialize(value=nil)
@value = value
@next_node = nil
end
def to_s
@value || nil
end
end
class SinglyLinkedList
attr_accessor :head
def initialize(first_value=nil)
# initializes the linked list
end
def insertAfter(node, new_node)
# adds a new node to the end of the list
end
def prepend(value)
#prepend to the beginning
end
def append(value)
# append to the end
end
def remove
# removes the first node
end
def last
# get's the last node
end
def length
# calculates the length of the list
end
def find(input)
# returns the node with that value or nil if none found
end
def reverse
# returns a new SinglyLinkedList with all elements reversed
end
def reverse!
# returns this SinglyLinkedList with all elements reversed
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment