-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0021.Merge Two Sorted Lists.swift
67 lines (56 loc) · 1.77 KB
/
0021.Merge Two 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
63
64
65
66
67
/**
* 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 Solution {
func mergeTwoLists(_ list1: ListNode?, _ list2: ListNode?) -> ListNode? {
if list1 == nil && list2 == nil {
return nil
}
if list1 == nil && list2 != nil {
return list2
}
if list1 != nil && list2 == nil {
return list1
}
if list1!.val <= list2!.val {
let next = mergeTwoLists(list1!.next, list2)
list1!.next = next
return list1
} else {
let next = mergeTwoLists(list1, list2!.next)
list2!.next = next
return list2
}
return nil
}
private func mergeTwoLists_v1(_ list1: ListNode?, _ list2: ListNode?) -> ListNode? {
var merged: ListNode? = ListNode()
var mergedListPointer = merged
var l1 = list1
var l2 = list2
while l1 != nil && l2 != nil {
if l2!.val <= l1!.val {
mergedListPointer?.next = l2
l2 = l2?.next
} else {
mergedListPointer?.next = l1
l1 = l1?.next
}
mergedListPointer = mergedListPointer?.next
}
if l1 == nil {
mergedListPointer?.next = l2
}
if l2 == nil {
mergedListPointer?.next = l1
}
return merged?.next
}
}