给定两个LeetCode上的问题
Linked List Cycle
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
Note: Do not modify the linked list.
1.如何判断一个链表是否有环
2.如果一个链表有环,找出这个环的开始节点
环检测(Cycle detection)
对于链表环检测问题可以使用”龟兔赛跑”算法来进行判断:即使用两个指针,一个指针为快指针(兔)一次移动两步,而另一个指针为慢指针(龟)一次移动一步。如果这个链表有环的话,那么这两个指针一定会在环上的某一点相遇。
首先定义一些变量,如图