feat: add Swift codes for hash_algorithm article (#576)

This commit is contained in:
nuomi1
2023-07-03 16:42:49 +08:00
committed by GitHub
parent 9b15072a85
commit 87076132e7
4 changed files with 124 additions and 1 deletions
+10 -1
View File
@@ -4,7 +4,7 @@
* Author: nuomi1 (nuomi1@qq.com)
*/
public class ListNode {
public class ListNode: Hashable {
public var val: Int //
public var next: ListNode? //
@@ -12,6 +12,15 @@ public class ListNode {
val = x
}
public static func == (lhs: ListNode, rhs: ListNode) -> Bool {
lhs.val == rhs.val && lhs.next.map { ObjectIdentifier($0) } == rhs.next.map { ObjectIdentifier($0) }
}
public func hash(into hasher: inout Hasher) {
hasher.combine(val)
hasher.combine(next.map { ObjectIdentifier($0) })
}
public static func arrToLinkedList(arr: [Int]) -> ListNode? {
let dum = ListNode(x: 0)
var head: ListNode? = dum