原题:
Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull
.
=>找到单向链表的环的起点,若没有环,返回null
Follow up:
Can you solve it without using extra space?
=>能否不使用额外的空间。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
}
};
晓东分析:
看单向链表中是否存在环,晓东之前已经讲过了。这篇文章主要讲一下,如何找到这个环的起点,其实方法很简单,就是在快慢指针相交的时候,把快指针移到开始,然后大家都以1步的步长开始运动&#