题解 | 判断链表中是否有环
/** * class ListNode(var `val`: Int) { * var next: ListNode? = null * } */ object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 * @return bool布尔型 */ fun hasCycle(head: ListNode?): Boolean { // write code here if(head == null) return false var slow = head var fast = head while(slow != null && fast?.next != null) { slow = slow?.next fast = fast?.next?.next if(slow === fast) { return true } } return false } }