衔接上一篇------leetcode142环形链表

    技术2025-10-22  15

    题目就不赘述了,直接思路和代码

    代码如下

    /** * Definition for singly-linked list. * function ListNode(val) { * this.val = val; * this.next = null; * } */ /** * @param {ListNode} head * @return {ListNode} */ var detectCycle = function(head) { if(!head||!head.next){ return null } let slow=head,fast=head while(fast&&fast.next){ slow=slow.next fast=fast.next.next if(slow===fast){ fast=head while(slow!==fast){ slow=slow.next fast=fast.next } return fast } } return null };

    运行结果

    Processed: 0.009, SQL: 9