142-linked-list-cycle-ii
Question
https://leetcode.com/problems/linked-list-cycle-ii/description/
Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull
.
Note:Do not modify the linked list.
Follow up:
Can you solve it without using extra space?
Example:
Thought Process
- Hash Table
- Use hash table to record the node visited
- Time complexity O(n)
- Space complexity O(n)
- Floyd's Tortoise and Hare
- Let's say that F is the length of noncyclic part, and C is the length of cycle
- Check article here, https://leetcode.com/articles/linked-list-cycle-ii/
- When tortoise reach the start of cycle, index 0, hare has traveled 2F step at F mod C index, call it index h
- If we move tortoise C - h steps, hare moves 2 (C - h) moves, they reach at the same index
- h + 2 (C - h) = 2C - h, taking the mod again, (2C - h) mod C = C - h
- Then we reinitialize one of the pointer back to the start of the list, move both pointers at the pace of 1
- They will collide at the start of cycle, let's say they meet at point a, index a in the loop
- 2 * distance(tortoise) = distance(hare)
- 2F + 2a = F + a + b + a + nC, where n >= 0
- F = nC + b, since a + b = C, moving from a F steps and moving from 0 F steps, the pointers collide at the entrance of cycle
- Time complexity O(n)
- Space complexity O(1)
Solution
public class Solution {
public ListNode detectCycle(ListNode head) {
ListNode slow = head, fast = head;
do {
if (fast == null || fast.next == null) return null;
slow = slow.next;
fast = fast.next.next;
} while (slow != fast);
fast = head;
while (fast != slow){
fast = fast.next;
slow = slow.next;
}
return slow;
}
}