为链表篇 增加 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

@@ -138,6 +138,32 @@ class Solution:
return low
```
Swift Code:
```swift
class Solution {
func reverseList(_ head: ListNode?) -> ListNode? {
// 边界条件
if head == nil || head?.next == nil {
return head
}
var pro = head
var low: ListNode?
while pro != nil {
// 代表橙色指针
var temp = pro
// 移动绿色指针
pro = pro?.next
// 反转节点
temp?.next = low
// 移动黄色指针
low = temp
}
return low
}
}
```
上面的迭代写法是不是搞懂啦现在还有一种递归写法不是特别容易理解刚开始刷题的同学可以只看迭代解法
@@ -227,6 +253,25 @@ class Solution:
return pro
```
Swift Code:
```swift
class Solution {
func reverseList(_ head: ListNode?) -> ListNode? {
// 结束条件
if head == nil || head?.next == nil {
return head
}
var pro = reverseList(head?.next)
// 将节点进行反转
head?.next?.next = head
// 防止循环
head?.next = nil
return pro
}
}
```
<br/>
> 贡献者[@jaredliw](https://github.com/jaredliw)注: