Skip to content
Permalink
Browse files

21

  • Loading branch information...
ACEMerlin committed Apr 13, 2019
1 parent b466e9b commit 9312844c743b11abbb2d61c9514f83b9b09a381f
Showing with 19 additions and 0 deletions.
  1. +19 −0 21.merge-two-sorted-lists.rb
@@ -24,6 +24,8 @@
# end


# Recursive.

# @param {ListNode} l1
# @param {ListNode} l2
# @return {ListNode}
@@ -38,3 +40,20 @@ def merge_two_lists(l1, l2)
l2
end
end


# Iterative.

def merge_two_lists(l1, l2)
ans = cur = ListNode.new(nil)
while !l1.nil? && !l2.nil?
if l1.val <= l2.val
cur.next, l1 = l1, l1.next
else
cur.next, l2 = l2, l2.next
end
cur = cur.next
end
cur.next = l1 || l2
ans.next
end

0 comments on commit 9312844

Please sign in to comment.
You can’t perform that action at this time.