142. Linked List Cycle II

202410111436
tags: #linked-list

var detectCycle = function(head) {
	let slow = head;
	let fast = head;
	while (fast && fast.next) {
		slow = slow.next;
		fast = fast.next.next;
		if (slow === fast) {
			slow = head;
			while (slow !== fast) {
				slow = slow.next;
				fast = fast.next;
			}
			return slow;
		}
	}
	return null;
};

Reference

https://leetcode.com/problems/linked-list-cycle-ii/