Skip to content

Instantly share code, notes, and snippets.

@arlandism
Created April 30, 2014 02:04
Show Gist options
  • Save arlandism/cc7d9f0734dd5f23b35f to your computer and use it in GitHub Desktop.
Save arlandism/cc7d9f0734dd5f23b35f to your computer and use it in GitHub Desktop.
node = @head.next_node
while !node.nil? do
node = node.next_node
end
node.next_node = Node.new(value)
@patrickgombert
Copy link

Here's an example append

  def append(value)
    if @head.nil? # special case
      @head = Node.new(value)
    else
      node = @head
      while @head.next_node # walk the tree until we are holding the last node
        node = node.next_node
      end
      new_end = Node.new(value) # create new last node (notice: it's next_node is nil)
      node.next_node = new_end # set our old end to point to the new end
    end
  end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment