Skip to content

Commit

Permalink
21
Browse files Browse the repository at this point in the history
  • Loading branch information
TedTran2019 committed Jul 8, 2022
1 parent e826485 commit c5b7963
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions ruby/21-Merge-Two-Sorted-Lists.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
def merge_two_lists(list1, list2)
return nil if list1.nil? && list2.nil?

dummy = ListNode.new
beginning = dummy
while list1 && list2
if list1.val > list2.val
dummy.next = list2
list2 = list2.next
else
dummy.next = list1
list1 = list1.next
end
dummy = dummy.next
end
if list1
dummy.next = list1
elsif list2
dummy.next = list2
end
beginning.next
end

0 comments on commit c5b7963

Please sign in to comment.