Skip to content

Instantly share code, notes, and snippets.

@workmad3
Last active August 29, 2015 14:05
Show Gist options
  • Save workmad3/119ea549fc21437128b4 to your computer and use it in GitHub Desktop.
Save workmad3/119ea549fc21437128b4 to your computer and use it in GitHub Desktop.
class LinkedList
def initialize(items)
items = Array(items)
if items.size == 1
@value = items[0]
else
@value, @next = items[0], LinkedList.new(items[1..-1])
end
end
def append(value)
if @next
@next.add(value)
else
@next = LinkedList.new(value)
end
end
def include?(value)
if @value == value
true
elsif @next
@next.include?(value)
else
false
end
end
def each(&blk)
yield @value
@next.each(&blk) if @next
end
def uniq(new_list = nil)
new_list ||= LinkedList.new(@value)
new_list.append(@value) unless new_list.include?(@value)
@next.uniq(new_list) if @next
new_list
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment