mirror of
https://github.com/chefyuan/algorithm-base.git
synced 2025-08-05 15:12:22 +00:00
为链表篇 增加 Swift 实现
This commit is contained in:
@@ -118,3 +118,20 @@ class Solution:
|
||||
return slow
|
||||
```
|
||||
|
||||
Swift Code:
|
||||
|
||||
```swift
|
||||
class Solution {
|
||||
func middleNode(_ head: ListNode?) -> ListNode? {
|
||||
var fast = head //快指针
|
||||
var slow = head //慢指针
|
||||
//循环条件,思考一下跳出循环的情况
|
||||
while fast != nil && fast?.next != nil {
|
||||
fast = fast?.next?.next
|
||||
slow = slow?.next
|
||||
}
|
||||
//返回slow指针指向的节点
|
||||
return slow
|
||||
}
|
||||
}
|
||||
```
|
||||
|
Reference in New Issue
Block a user