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

Follow up:

思路:
1)同linked-list-cycle-i一题,使用快慢指针方法,判定是否存在环,并记录两指针相遇位置(Z);
2)将两指针分别放在链表头(X)和相遇位置(Z),并改为相同速度推进,则两指针在环开始位置相遇(Y)。
证明如下:
如下图所示,X,Y,Z分别为链表起始位置,环开始位置和两指针相遇位置,则根据快指针速度为慢指针速度的两倍,可以得出:
2*(a + b) = a + b + n * (b + c);即
a=(n - 1) * b + n * c = (n - 1)(b + c) +c;
注意到b+c恰好为环的长度,故可以推出,如将此时两指针分别放在起始位置和相遇位置,并以相同速度前进,当一个指针走完距离a时,另一个指针恰好走出
绕环n-1圈加上c的距离。
故两指针会在环开始位置相遇。
class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        if(head == NULL){
            return 0;
        }
        ListNode* slow = head;
        ListNode* fast = head;
        while(fast != NULL && fast->next != NULL){
            slow = slow->next;
            fast = fast->next->next;
            if(slow == fast){
                break;
            }
        }
        if(fast == NULL || fast->next == NULL){
            return NULL;
        }
        slow = head;
        while(slow != fast){
            slow = slow->next;
            fast = fast->next;
        }
        return slow;
    }
};

Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull. Follow up: Can的更多相关文章

  1. [LeetCode OJ] Linked List Cycle II—Given a linked list, return the node where the cycle begins. If there is no cycle, return null.

    /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...

  2. [Linked List]Remove Nth Node From End of List

    Total Accepted: 84303 Total Submissions: 302714 Difficulty: Easy Given a linked list, remove the nth ...

  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]Linked List Cycle & Linked List Cycle II——单链表中的环

    题目要求 Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up: Can you so ...

  5. 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 ...

  6. 【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 ...

  7. 142. Linked List Cycle II

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

  8. Leetcode Linked List Cycle II

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

  9. 15. Linked List Cycle && Linked List Cycle II

    Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up: Can you solve i ...

随机推荐

  1. nginx 配置步骤

    D:\myphp2017\nginx\conf.nginx.conf37行 吧localhost 改为www.ff.com41行取消注释44行 加D:\myphp2017\nginx\html45 在 ...

  2. JsonConvert对象实现json与对象之间的转换

    自己下载Newtonsoft.Json文件 使用JsonConvert对象转换 1. 2.添加引用到项目中,然后导入命名空间 3.就可以使用JsonConvert对象实现Json与类型之间的转换

  3. PL/SQL 多表关联UPDATE

    假设有两个表A和B,A表字段a,b,c,d,B表字段b,e,f,两表的关联条件是字段b,现在想做个data patch,欲将B表中的字段e的值patch给A表的字段c. 有如下两种方法: 1 upda ...

  4. ES-Mac OS环境搭建(2)

    下载 进入官网,选择downloads进入下载页. 选择elasticsaerch下载. 新的页面中,下拉选择历史版本. 下拉选择elasticsearch和版本,然后点击下载. 选择MACOS/LI ...

  5. SQLServer查询语句收集(非常实用)

    =============================    SQLServer语句收集1  =========================== 1.数据操作  Select      --从 ...

  6. 阻止除root外的其他用户登录

    在对系统进行某些更新时,你可能不希望用户登录,这时可以使用/ e t c / n o l o g i n文件,大多数系统都提供这个文件.一旦在/ e t c目录中使用t o u c h命令创建了一个名 ...

  7. path与classpath区别(转)

    转自http://blog.csdn.net/mydreamongo/article/details/8155408 1.path的作用 path是系统用来指定可执行文件的完整路径,即使不在path中 ...

  8. github入门之配置github本地仓库--2

    *前期准备工作 创建github账户 github地址 1.设置SSH_Key ssh-keygen -t rsa -C "你的邮箱" 2.查看秘钥 cat ~/.ssh/id_r ...

  9. linux 删除文件后空间没有释放的解决办法

    清空没用的文件,当我删除文件后,发现可用空间沒有变化 os:centos4.7 现象: 发现当前磁盘空间使用情况: [root@ticketb ~]# df -hFilesystem          ...

  10. Codeforces C The Game of Efil (暴力枚举状态)

    http://codeforces.com/gym/100650 阅读题,边界的cell的邻居要当成一个环形的来算,时间有8s,状态最多2^16种,所以直接暴力枚举就行了.另外一种做法是逆推. #in ...