Created
December 22, 2013 15:01
-
-
Save Mon-Ouie/8083749 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
start.include? x | |
end | |
def each(&block) | |
return to_enum(:each) unless block | |
@lists[0].each(&block) if @lists[0] | |
end | |
def insert(x) | |
if !@lists[0] || x < @lists[0].head | |
prev = x | |
@lists.map! do |rest| | |
head = LinkedList.new(prev, rest) | |
prev = Node.new(x, head) | |
head | |
end | |
else | |
nodes = [] | |
i = @lists.size - 1 | |
start = @lists[i] | |
while i >= 1 | |
node = start.each_node.find { |n| | |
n.tail.nil? or n.tail.head.value >= x | |
} | |
nodes << node | |
start = node.head.below | |
i -= 1 | |
end | |
bottom = start.each_node.find { |n| | |
n.tail.nil? or n.tail.head >= x | |
}.insert(x) | |
i = nodes.size - 1 | |
while rand(2).zero? && i >= 0 | |
bottom = nodes[i].insert Node.new(x, bottom) | |
i -= 1 | |
end | |
if i < 0 | |
while rand(2).zero? | |
@lists << LinkedList.new(Node.new(@lists[0].head, @lists.last), nil) | |
bottom = @lists.last.insert Node.new(x, bottom) | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment