We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 22acf03 commit 817d04cCopy full SHA for 817d04c
solution/021.Merge Two Sorted Lists/Solution.rb
@@ -0,0 +1,39 @@
1
+# Definition for singly-linked list.
2
+# class ListNode
3
+# attr_accessor :val, :next
4
+# def initialize(val)
5
+# @val = val
6
+# @next = nil
7
+# end
8
9
+
10
+# @param {ListNode} l1
11
+# @param {ListNode} l2
12
+# @return {ListNode}
13
+def merge_two_lists(l1, l2)
14
+ return l1 if l2.nil?
15
+ return l2 if l1.nil?
16
17
+ head = ListNode.new(0)
18
+ temp = head
19
20
+ until l1.nil? && l2.nil?
21
+ if l1.nil?
22
+ head.next = l2
23
+ break
24
+ elsif l2.nil?
25
+ head.next = l1
26
27
+ elsif l1.val < l2.val
28
+ head.next = ListNode.new(l1.val)
29
+ head = head.next
30
+ l1 = l1.next
31
+ else
32
+ head.next = ListNode.new(l2.val)
33
34
+ l2 = l2.next
35
+ end
36
37
+ temp.next
38
+end
39
solution/023.Merge k Sorted Lists/Solution.rb
0 commit comments