-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path21_merge_two_sorted_lists.swift
58 lines (48 loc) · 2.02 KB
/
21_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
/**
* 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? {
let defaultNode = ListNode(-101)
var firstNode: ListNode = list1 ?? defaultNode
var secondNode: ListNode = list2 ?? defaultNode
var newListNode = ListNode()
if firstNode.val == -101 && secondNode.val == -101 {
return nil
} else if firstNode.val == -101 {
newListNode.val = secondNode.val
if let secondNodeNext = secondNode.next {
newListNode.next = self.mergeTwoLists(firstNode, secondNodeNext)
}
} else if secondNode.val == -101 {
newListNode.val = firstNode.val
if let firstNodeNext = firstNode.next {
newListNode.next = self.mergeTwoLists(firstNodeNext, secondNode)
}
} else { // list1 and list2 both exist
if firstNode.val <= secondNode.val {
newListNode.val = firstNode.val
if let firstNodeNext = firstNode.next {
newListNode.next = self.mergeTwoLists(firstNodeNext, secondNode)
} else {
newListNode.next = self.mergeTwoLists(nil, secondNode)
}
} else if firstNode.val > secondNode.val {
newListNode.val = secondNode.val
if let secondNodeNext = secondNode.next {
newListNode.next = self.mergeTwoLists(firstNode, secondNodeNext)
} else {
newListNode.next = self.mergeTwoLists(firstNode, nil)
}
}
}
return newListNode
}
}