1. Linked List Cycle II My Submissions QuestionEditorial Solution

    Total Accepted: 74093 Total Submissions: 235430 Difficulty: Medium

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

Note: Do not modify the linked list.

Follow up:

Can you solve it without using extra space?

思路:假设快慢指针在x处相遇,

此时快指针在环内假设走了n圈加x步,而总的步数:nr+x+a

是慢指针的2倍

即有nr+x+a =(x+a)*2

上式为a=(n-1)r+r-x;

说明慢指针走到a时候,快指针从x处走了r-x前一个加上n-1圈的距离,

注意此时快指针从X处一步一步走,慢指针也一步一步走,直到在入口相遇

时间复杂度:O(n)

空间复杂度:O(1)

/**
* 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) {
if(head==NULL||head->next==NULL)return NULL;
ListNode *p_quick=head,*p_slow=head;
while(p_quick!=NULL&&p_quick->next!=NULL){
p_quick = p_quick->next->next;
p_slow = p_slow->next;
if(p_quick==p_slow)break;
}
if(p_quick==p_slow){
p_slow=head;
while(p_slow!=p_quick){
p_slow = p_slow->next;
p_quick=p_quick->next;
}
return p_slow;
}
else return NULL;
}
};

53-Linked List Cycle II的更多相关文章

  1. [算法][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 ...

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

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

  4. LeetCode解题报告:Linked List Cycle && Linked List Cycle II

    LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...

  5. 【LeetCode练习题】Linked List Cycle II

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

  6. [Linked List]Linked List Cycle,Linked List Cycle II

    一.Linked List Cycle Total Accepted: 85115 Total Submissions: 232388 Difficulty: Medium Given a linke ...

  7. Linked List Cycle && Linked List Cycle II

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

  8. LeetCode之“链表”:Linked List Cycle && Linked List Cycle II

    1.Linked List Cycle 题目链接 题目要求: Given a linked list, determine if it has a cycle in it. Follow up: Ca ...

  9. 141. Linked List Cycle&142. Linked List Cycle II(剑指Offer-链表中环的入口节点)

    题目: 141.Given a linked list, determine if it has a cycle in it. 142.Given a linked list, return the ...

  10. LeetCode Linked List Cycle II 和I 通用算法和优化算法

    Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...

随机推荐

  1. BUAA-OO-JML

    BUAA-OO-JML JML 概念与 toolchain JML 是一种为 Java 程序设计的.遵循 design by contract 范式的.基于 Hoare Logic 构建的 behav ...

  2. Android 服务名称规则invalid service name 限制16字符以内

    今天调试网络服务的时候为了区分,修改了原有服务名称,同时新增了两个服务. 系统运行的时候报错找不到对应的服务 init: no such service 'wpa_supplicant_common' ...

  3. SI Macro

    获取 buf 里的 symbol cbuf = BufListCount() msg(cbuf) ibuf = 0 while (ibuf < cbuf) { hbuf = BufListIte ...

  4. udev 使用方法

    原文地址 http://blog.163.com/againinput4@yeah/blog/static/122764271200962305339483/ 最近有在研究SD卡设备节点自动创建及挂载 ...

  5. linux 安装rabbitmq

    1.安装rabbitmq会依赖erlang.socat.unixodbc 下载 unixODBC-2.3.7.tar.gz ,创建路径/usr/local/unixODBC-2.3.7,解压到该路径下 ...

  6. git与pycharm的使用详解(git+gitlab+pycham)

    前言 当自动化框架搭建出来后,需要多个人来使用框架,写自动化用例. 在这个阶段,我们不可能将写好的代码打包发给其他人,这样很麻烦,多人协作一点也不灵活. 这时候,就提现出了git的价值 一.下载安装 ...

  7. Redis网络库源码分析(3)之ae.c

    一.aeCreateEventLoop & aeCreateFileEvent 上一篇文章中,我们已经将服务器启动,只是其中有些细节我们跳过了,比如aeCreateEventLoop函数到底做 ...

  8. 攻防世界 WEB 高手进阶区 unserialize3 Writeup

    攻防世界 WEB 高手进阶区 unserialize3 Writeup 题目介绍 题目考点 PHP反序列化 __wakeup漏洞 Writeup 题名 unserialize 是反序列化函数名 了解一 ...

  9. Python基础入门(1)- Python环境搭建与基础语法

    Python编程环境搭建 Python环境搭建 官网下载:https://www.python.org/ python --version PyCharm下载安装 安装 官网下载:https://ww ...

  10. 一款吊炸天的AI图片增强工具!

    背景 如果你工作中需要制作文档,PPT,或者给文章配图,或者需要制作视频.一定会有在网上寻找图片素材的经历. 但网上的图质量参差不一,有时候找到了喜欢的图,但是质量不行,分辨率太低. 有的人就忍了,但 ...