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 (详细地址:https://leetcode.com/problems/linked-list-cycle/description/)

思路分析:我第一次见到这个题目的时候是在《剑指offer》这本书上,第一次见到的时候也是比较惊讶的。不过第二次见到直接有知道怎么解决了,具体的可以参见环检测(龟兔赛跑)问题 ,里面给出了详细的介绍和解答。需要详细介绍的同学可以参考我的这篇博客Find the Duplicate Number,里面我也举了一个详细的例子,另外给出了点证明。在这我就不多说了,只要思路就是一个指针(slow)一次移动一步,一个指针(fast)一次移动两步,直到它们在环中的一点相遇。

代码:

Leetcode之Linked List Cycle 问题