Skip to content

Instantly share code, notes, and snippets.

@kinduff
Last active March 24, 2016 17:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kinduff/85c436ff2d0cbed97e60 to your computer and use it in GitHub Desktop.
Save kinduff/85c436ff2d0cbed97e60 to your computer and use it in GitHub Desktop.
class LinkedList
def initialize
@head = @tail = nil
end
def add(value)
node = Node.new(value)
@head = node if @head.nil?
@tail.next = node unless @tail.nil?
@tail = node
end
end
class Node
def initialize(value)
@value = value
@next = nil
end
def next
@next
end
def next=(value)
@next = value
end
end
list = LinkedList.new()
list.add(1)
# => current: 1, head: true, tail: true, next: nil
list.add(2)
# => current: 2, head: false, tail: true, next: nil
list.add(3)
# => current: 3, head: false, tail: true, next: nil
# Final Result
# Current: 1, head: true, tail: false, next: 2
# Current: 2, head: false, tail: false, next: 3
# Current: 3, head: false, tail: true, next: nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment