Skip to main content

142. Linked List Cycle II

分析

Pasted image 20220503234416.png

题解

function detectCycle(head: ListNode | null): ListNode | null {
let slow = head, fast = head

while (slow && fast) {
slow = slow.next
fast = fast.next?.next

if (slow === fast) {
fast = head

while (fast !== slow) {
fast = fast.next
slow = slow.next
}
return slow
}
}

return null
};