2 Star 2 Fork 1

Justin Yuan/LeetCode-Swift

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
ReorderList.swift 1.63 KB
一键复制 编辑 原始数据 按行查看 历史
/**
* Question Link: https://leetcode.com/problems/reorder-list/
* Primary idea: Use Runner Tech to split the list, reverse the second half, and merge them
* Time Complexity: O(n), Space Complexity: O(1)
*
* Definition for singly-linked list.
* public class ListNode {
* public var val: Int
* public var next: ListNode?
* public init(_ val: Int) {
* self.val = val
* self.next = nil
* }
* }
*/
class ReorderList {
func reorderList(head: ListNode?) {
guard let head = head else {
return
}
var prev: ListNode? = head
var post: ListNode? = head
_split(&prev, &post)
prev = head
post = _reverse(&post)
_merge(&prev, &post)
}
private func _split(inout prev: ListNode?, inout _ post: ListNode?) {
while post != nil && post!.next != nil {
prev = prev!.next
post = post!.next!.next
}
post = prev!.next
prev!.next = nil
}
private func _reverse(inout head: ListNode?) -> ListNode? {
var prev = head
var temp: ListNode?
while prev != nil {
let post = prev!.next
prev!.next = temp
temp = prev
prev = post
}
return temp
}
private func _merge(inout prev: ListNode?, inout _ post: ListNode?) {
while prev != nil && post != nil{
let preNext = prev!.next
let posNext = post!.next
prev!.next = post
post!.next = preNext
prev = preNext
post = posNext
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/BiggerMax/LeetCode-Swift.git
git@gitee.com:BiggerMax/LeetCode-Swift.git
BiggerMax
LeetCode-Swift
LeetCode-Swift
master

搜索帮助