Leetcode 142:Linked List Cycle II(环形链表 II)

题目:Linked List Cycle II(环形链表 II)

Given a linked list, return the node where the cycle begins. If there is no cycle, return null.

There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail’s next pointer is connected to. Note that pos is not passed as a parameter.

Notice that you should not modify the linked list.

Follow up:

Can you solve it using O(1) (i.e. constant) memory?

题解


此处的slow指针应该是跑不完这个环的。

这里fast指针和slow指针的起始位置很重要;正如下面评论所说,开始位置不是head,则前面的推到结果就没法适用了。