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

Follow up:
Can you solve it without using extra space?

题意:

给定一个链表,找到环起始的位置。如果环不存在,返回NULL。

分析:

(1)首先要判断该链表是否有环。如果没有环,那么返回NULL。

(2)其次,当已知环存在后,寻找环起始的位置。

思路:

(1)链表是否有环,利用快慢指针很好解决。

(2)当环存在时,寻找环的起始位置:

++++使用额外的空间,可以用一个map存放链表的节点,遍历链表:

如果当前节点在map中没有出现,那么将该节点加入到map中,否则,返回当前节点(也就是环的起点)。

-----不使用额外空间,利用快慢指针的特性。

1st ruond:
SP(start point) MP(match point)
A: --------------------------------------------------------MP
B: - - - - - - - - - - - - - MP 2nd round:
B: MP- - - - - -- - - - - - - - -MP
C:---------------------------------------------------------MP

3rd round:
B1:- - - - - - - - - - - - - MP
B2: MP- - - - - - - - - - - - - - MP B1与B2必定会在途中相遇。 那么,他们首次相遇的点即为环的起始点。

设定两个指针A,B都从SP(Start point)出发: A每次向后移动两个节点,B每次向后移动一个节点。

假设A,B在MP(match point)相遇。 那么A所走的路径长度为B所走的路径长度的2倍:La = 2*Lb。

假设有另一个指针C还是从SP启动,每次向后移动两个节点, B从MP开始出发每次向后移动一个节点,那么可以想象C和B会在MP相遇。

如果C从SP出发,每次只移动一个节点,B依旧从MP出发且每次向后移动一个节点, 那么B和C依旧会在MP相遇。

class Solution {
public:
ListNode *detectCycle(ListNode *head) {
// list length less than two
if(head==NULL || head->next==NULL) return NULL;
// if cycle exists in the list
ListNode *startA= head->next;
ListNode *endA= head->next->next;
while(endA!=NULL && endA != startA){
endA=endA->next;
if(endA){
endA=endA->next;
startA=startA->next;
}
}
// no cycle exists
if(endA==NULL) return NULL;
// find a cycle then search the start point
ListNode *match = startA;
ListNode *start = head;
while(start!=match){
start = start->next;
match = match->next;
}
return start;
}
};
作者:Double_Win
由于本人水平有限,文章在表述和代码方面如有不妥之处,欢迎批评指正~
 

[LeetCode 题解]: Linked List Cycle II的更多相关文章

  1. [Leetcode Week6]Linked List Cycle II

    Linked List Cycle II 题解 题目来源:https://leetcode.com/problems/linked-list-cycle-ii/description/ Descrip ...

  2. Java for LeetCode 142 Linked List Cycle II

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

  3. 【Leetcode】Linked List Cycle II

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

  4. [LeetCode] 142. Linked List Cycle II 链表中的环 II

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

  5. 【题解】【链表】【Leetcode】Linked List Cycle II

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

  6. [LeetCode] 142. Linked List Cycle II 单链表中的环之二

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

  7. (链表 双指针) leetcode 142. Linked List Cycle II

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

  8. leetcode 142. Linked List Cycle II

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

  9. 【leetcode】Linked List Cycle II (middle)

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

随机推荐

  1. 【ZZ】各类程序开发语言概述 | 菜鸟教程

    http://www.runoob.com/w3cnote/programming-intro.html 各类程序开发语言概述, 点击查看大图:

  2. php error_log记录日志的使用方法和配置 (日志目录一定要手动创建)

    对于PHP开发者来 说,一旦某个产品投入使用,应该立即将 display_errors选项关闭,以免因为这些错误所透露的路径.数据库连接.数据表等信息而遭到黑客攻击.但是,任何一个产品在投入使用后,都 ...

  3. JS截取字符串常用方法详细整理&&MYSQL

    截取字符串的使用比较广泛,有很多中方法,本文粗略的整理了一些,感兴趣的额朋友可以才参考下 使用 substring()或者slice() 函数:split() 功能:使用一个指定的分隔符把一个字符串分 ...

  4. 使用PHP自带zlib函数 几行代码实现PHP文件打包下载zip

    <?php //获取文件列表 function list_dir($dir){ $result = array(); if (is_dir($dir)){ $file_dir = scandir ...

  5. SQL Server修改表结构后批量更新所有视图

    最近修改了数据库表结构,数据同步的时候出了问题,发现很多数据明明已经修改,但是通过视图筛选出来的还是原来的数据,所以怀疑应该是视图缓存了数据,在园子里找到下面的博文,在这里做个记录备忘. 原文链接:h ...

  6. 配置mysql环境变量

    配置mysql环境变量(非必要) 说明:给mysql配置环境变量后我们就可以在cmd里运行mysql(开启.停止等操作) 1. 和其实环境变量的配置方法一样,我们打开环境变量配置窗口(组合键win+P ...

  7. php时间轴函数,很不错,记下了

    function TranTime($time) { //$time = strtotime($time); $nowTime = time (); $message = ''; if(empty($ ...

  8. Python实践练习:将一个文件夹备份到一个 ZIP 文件

    题目 项目要求:假定你正在做一个项目,它的文件保存在 C:\AlsPythonBook 文件夹中.你担心工作会丢失, 所以希望为整个文件夹创建一个 ZIP 文件, 作为"快照" . ...

  9. Windows环境下为PHP5.6安装redis扩展和memcached扩展

    一.php安装redis扩展   1.使用phpinfo()函数查看PHP的版本信息,这会决定扩展文件版本       2.根据PHP版本号,编译器版本号和CPU架构, 选择php_redis-2.2 ...

  10. Selenium Webdriver——AutoIt和robot实现右键另存

    方案如下: 1.selenium 弹出右键菜单 2.robot选择相关菜单 3.调用autoIt实现windows gui另存操作 test case 如下: 1.打开百度(谷歌浏览器) 2.选择百度 ...