Leetcode之Linked List Cycle II 问题
问题描述:
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?
问题来源:Linked List Cycle II (详细地址:https://leetcode.com/problems/linked-list-cycle-ii/description/)
思路分析:这道题就在Linked List Cycle 的基础上多了一步,就是找到环的入口。同理,这道题的解法和相关原理已经在Find the Duplicate Number 中详细介绍了。在这我只负责简单地介绍一下步骤:
第一步:一个快指针一个慢指针找到环中的一个点;
第二步:让其中一个指针从头开始,另外一个原地不动,然后每次都往前移动一步,再次相遇的时候就是入口。
代码:
题目给定的节点的定义:
具体解法: