Last active
March 24, 2016 17:14
-
-
Save kinduff/85c436ff2d0cbed97e60 to your computer and use it in GitHub Desktop.
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 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