给出一个链表,若其中包含环,请找出该链表的环的入口节点,否则输出null
var findNode = (head)=>{
if(head.next == null || head == null){
return null
}
var slow = head
var fast = head
while(fast != slow){
if(fast == null || fast.next == null){
return null
}
fast = fast.next.next
slow = slow.next
}
// 环的长度
let length = 1
let temp = slow
slow = slow.next
if(slow != temp){
length++
}
// 找到环的位置
fast =slow = head
while(length-->0){
fast = fast.next
}
while(fast == slow){
slow = slow.next
fast = fast.next
}
return slow
}