2023-01-06 03:39:19 +08:00
|
|
|
/**
|
2023-01-02 21:48:32 +08:00
|
|
|
* File: ListNode.swift
|
|
|
|
* Created Time: 2023-01-02
|
|
|
|
* Author: nuomi1 (nuomi1@qq.com)
|
|
|
|
*/
|
|
|
|
|
2023-07-03 16:42:49 +08:00
|
|
|
public class ListNode: Hashable {
|
2023-04-09 04:32:17 +08:00
|
|
|
public var val: Int // 节点值
|
|
|
|
public var next: ListNode? // 后继节点引用
|
2023-01-02 21:48:32 +08:00
|
|
|
|
|
|
|
public init(x: Int) {
|
|
|
|
val = x
|
|
|
|
}
|
2023-01-30 15:43:29 +08:00
|
|
|
|
2023-07-03 16:42:49 +08:00
|
|
|
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) })
|
|
|
|
}
|
|
|
|
|
2023-01-30 15:43:29 +08:00
|
|
|
public static func arrToLinkedList(arr: [Int]) -> ListNode? {
|
|
|
|
let dum = ListNode(x: 0)
|
|
|
|
var head: ListNode? = dum
|
|
|
|
for val in arr {
|
|
|
|
head?.next = ListNode(x: val)
|
|
|
|
head = head?.next
|
|
|
|
}
|
|
|
|
return dum.next
|
|
|
|
}
|
2023-01-02 21:48:32 +08:00
|
|
|
}
|