import Foundation
class SinglyLinkListNode {
let value: Int
var next: SinglyLinkListNode?
init(_ value: Int) {
self.value = value
next = nil
}
}
struct SinglyLinkList {
var head: SinglyLinkListNode?
var tail: SinglyLinkListNode? {
guard
head != nil
else { return nil }
var currentNode = head
while currentNode?.next != nil {
currentNode = currentNode?.next
}
return currentNode
}
func displayMiddle() {
guard head != nil else { return }
var slow = head
var fast = head
while fast != nil {
slow = slow?.next
fast = fast?.next?.next
}
if let slow = slow {
print("Link list middle value is \(slow.value)")
}
}
}