给出一个链表,若其中包含环,请找出该链表的环的入口节点,否则输出null

    1. var findNode = (head)=>{
    2. if(head.next == null || head == null){
    3. return null
    4. }
    5. var slow = head
    6. var fast = head
    7. while(fast != slow){
    8. if(fast == null || fast.next == null){
    9. return null
    10. }
    11. fast = fast.next.next
    12. slow = slow.next
    13. }
    14. // 环的长度
    15. let length = 1
    16. let temp = slow
    17. slow = slow.next
    18. if(slow != temp){
    19. length++
    20. }
    21. // 找到环的位置
    22. fast =slow = head
    23. while(length-->0){
    24. fast = fast.next
    25. }
    26. while(fast == slow){
    27. slow = slow.next
    28. fast = fast.next
    29. }
    30. return slow
    31. }