题目 - Linked List Cycle
难度:中等
图示分析

代码
判断链表是否有环
func hasCycle(_ head: ListNode?) -> Bool {
var fast = head, slow = head
while fast?.next != nil && fast?.next?.next != nil {
fast = fast?.next?.next
slow = slow?.next
if slow === fast {
return true
}
}
return false
}
环形链表的第一个节点
func detectCycle(_ head: ListNode?) -> ListNode? {
var fast = head, slow = head
while fast != nil {
slow = slow?.next
fast = fast?.next?.next
if slow === fast {
slow = head
while slow !== fast {
slow = slow?.next
fast = fast?.next
}
return slow
}
}
return nil
}