-
Notifications
You must be signed in to change notification settings - Fork 0
/
23_merge_n_sorted_lists.rb
71 lines (57 loc) · 1.1 KB
/
23_merge_n_sorted_lists.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# Merge k Sorted Lists
#
# Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
#
# Example:
#
# Input:
# [
# 1->4->5,
# 1->3->4,
# 2->6
# ]
# Output: 1->1->2->3->4->4->5->6
class ListNode
attr_accessor :val, :next
def initialize(val)
@val = val
@next = nil
end
end
def merge_k_lists(lists)
return [] if lists.empty?
lists.compact!
res = r = ListNode.new(nil)
loop do
min = 1.0/0.0
min_list = nil
index = nil
lists.each_with_index do |l, i|
if l && l.val < min
min = l.val
min_list = l
index = i
end
end
break if min_list == nil
if min_list.next
lists[index] = min_list.next
else
lists.delete_at(index)
end
r.val = min
unless lists.empty?
r.next = r = ListNode.new(nil)
end
end
res
end
l11 = ListNode.new(1)
l12 = l11.next = ListNode.new(4)
l12.next = ListNode.new(5)
l21 = ListNode.new(1)
l22 = l21.next = ListNode.new(3)
l22.next = ListNode.new(4)
l31 = ListNode.new(2)
l31.next = ListNode.new(6)
p merge_k_lists([l11, l21, l31])