为链表篇 增加 Swift 实现

This commit is contained in:
frank-tian
2021-07-17 22:28:06 +08:00
parent a16c030b44
commit 7b55df11dc
14 changed files with 499 additions and 3 deletions

View File

@@ -123,3 +123,27 @@ class Solution:
return headtemp.next
```
Swift Code
```swift
class Solution {
func mergeTwoLists(_ l1: ListNode?, _ l2: ListNode?) -> ListNode? {
var l1 = l1, l2 = l2
var headpro: ListNode? = ListNode(-1)
var headtemp = headpro
while l1 != nil && l2 != nil {
//接上大的那个
if l1!.val >= l2!.val {
headpro?.next = l2
l2 = l2!.next
} else {
headpro?.next = l1
l1 = l1!.next
}
headpro = headpro?.next
}
headpro?.next = l1 != nil ? l1 : l2
return headtemp?.next
}
}
```