-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0023-merge-k-sorted-lists.swift
62 lines (52 loc) · 1.57 KB
/
0023-merge-k-sorted-lists.swift
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
/**
* Question Link: https://leetcode.com/problems/merge-k-sorted-lists/
*/
/**
* Definition for singly-linked list.
* public class ListNode {
* public var val: Int
* public var next: ListNode?
* public init() { self.val = 0; self.next = nil; }
* public init(_ val: Int) { self.val = val; self.next = nil; }
* public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
* }
*/
class MergeKLists {
func mergeKLists(_ lists: [ListNode?]) -> ListNode? {
guard lists.contains(where: { $0 != nil }) else { return nil }
var lists = lists
while lists.count > 1 {
var mergedLists = [ListNode?]()
for i in stride(from: 0, to: lists.count, by: 2) {
let l1 = lists[i]
let l2 = (i + 1 < lists.count) ? lists[i + 1] : nil
mergedLists.append(self.merge(l1: l1, l2: l2))
}
lists = mergedLists
}
return lists.first!
}
func merge(l1: ListNode?, l2: ListNode?) -> ListNode? {
var dummy = ListNode()
var tail: ListNode? = dummy
var l1 = l1
var l2 = l2
while l1 != nil && l2 != nil {
if l1!.val < l2!.val {
tail?.next = l1
l1 = l1?.next
} else {
tail?.next = l2!
l2 = l2?.next
}
tail = tail?.next
}
if l1 != nil {
tail?.next = l1
}
if l2 != nil {
tail?.next = l2
}
return dummy.next
}
}