Skip to content

Solution in Ruby 021 #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions solution/021.Merge Two Sorted Lists/Solution.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Definition for singly-linked list.
# class ListNode
# attr_accessor :val, :next
# def initialize(val)
# @val = val
# @next = nil
# end
# end

# @param {ListNode} l1
# @param {ListNode} l2
# @return {ListNode}
def merge_two_lists(l1, l2)
return l1 if l2.nil?
return l2 if l1.nil?

head = ListNode.new(0)
temp = head

until l1.nil? && l2.nil?
if l1.nil?
head.next = l2
break
elsif l2.nil?
head.next = l1
break
elsif l1.val < l2.val
head.next = ListNode.new(l1.val)
head = head.next
l1 = l1.next
else
head.next = ListNode.new(l2.val)
head = head.next
l2 = l2.next
end
end
temp.next
end

Empty file.