ARTICLE DETAIL

资讯详情

深耕网站建设、视觉设计与SEO优化的一线实战洞察。

[swift 外部干涉法 extension]

[swift 外部干涉法 extension]
/*** 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*     }* }*/
// ListNode 要实现以下两个,如何外部干涉
// Equatable
// Hashable
extension ListNode: Equatable {public static func == (l: ListNode, r: ListNode) -> Bool {return l === r}
}extension ListNode: Hashable {public func hash(into hasher: inout Hasher) {hasher.combine(ObjectIdentifier(self))}
}// 快慢指针,快指针 追上慢指针
class Solution {func hasCycle(_ head: ListNode?) -> Bool {var slow: ListNode? = head var fast: ListNode? = head while fast?.next != nil {slow = slow?.nextfast = fast?.next?.nextif fast == slow {return true}}return false}
}
返回列表