Skip to content

Commit

Permalink
wrote add_in_order method for linked_list
Browse files Browse the repository at this point in the history
  • Loading branch information
add2point71dots committed May 4, 2017
1 parent c95edb1 commit 62e5789
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/linked_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,27 @@ def add(value)
self
end

def add_in_order(value)
if @size == 0
@head = Node.new(value)
elsif value < @head.data
@head = Node.new(value, @head)
else
current = @head
loop do
if current.next == nil
current.next = Node.new(value)
break
elsif current.next.data >= value
current.next = Node.new(value, current.next)
break
end
current = current.next
end
end
@size += 1
end

def delete(val)
return nil if @size == 0
if @head.data == val
Expand Down

0 comments on commit 62e5789

Please sign in to comment.