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. hadoop启动时,报ssh: Could not resolve hostname xxx: Name or service not known

    本文转载自:http://blog.csdn.net/wodewutai17quiet/article/details/76795951 问题:hadoop启动时,报ssh: Could not re ...

  2. Oracle VM VirtualBox虚拟机安装Ubuntu Server

    安装过程如下:原文转自:http://www.linuxidc.com/Linux/2012-04/59368p8.htm

  3. 模板引擎文档 - layui.laytpl 介绍

    <!DOCTYPE html> <html class="ui-page-login"> <head> <meta charset=&qu ...

  4. 【BZOJ】1057 [ZJOI2007]棋盘制作(悬线法)

    题目 传送门:QWQ 分析 先把题目给出的矩阵变换一下,如果$ a[i][j] $中$ i+j \mod 2 = 1 $那么就对$ a[i][j] $取一下反. 接着就是求原图中最大的0.1子矩阵 详 ...

  5. Linux 性能分析的前 60 秒

    编译自:http://techblog.netflix.com/2015/11/linux-performance-analysis-in-60s.html作者: Brendan Gregg转载自:h ...

  6. Util-linux-ng-2.17

    yum install -y util-linux-ng 即可安装Util-linux-ng,其中包含了非常多的软件 Util-linux-ng 的内容 安装的程序:addpart, agetty, ...

  7. delphi 组件容器TObjectList代替List

    delphi 组件容器TObjectList代替List TObjectList objList->delete(0); 这个会释放第0行元素的对象 class TTabFormInfo { p ...

  8. MyEclipse 配置Android环境

    eclipse https://www.eclipse.org/downloads/download.php?file=/oomph/epp/mars/R2/eclipse-inst-win64.ex ...

  9. eclipse怎么自定义工具栏

    1.点击透视图按钮---->右键---->Customize: 2.勾选或者去掉相关项目:

  10. html 原生tab切换js

    $("#ulId li").on('click', function () { $("#li-container").children().hide(); $( ...