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

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

C++

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* detectCycle2(ListNode *head){
ListNode *fast = head;
ListNode *slow = head;
if(head==NULL) return NULL;
while (NULL != fast){
if (fast != NULL) fast = fast->next;
if (fast != NULL) fast = fast->next;
if (slow != NULL) slow = slow->next;
while(fast != NULL && slow == fast){
slow = head;
while(slow!=fast){
fast = fast->next;
slow = slow->next;
}
return slow;
}
}
return NULL;
} ListNode *detectCycle(ListNode *head) {
ListNode *fast = head;
ListNode *slow = head;
if (NULL == head) return NULL;
while(fast && fast->next){
fast = fast->next->next;
slow = slow->next;
if (fast != NULL && slow == fast)
return FindTheFirstSameNode(fast,head);
}
return NULL;
} ListNode *FindTheFirstSameNode(ListNode* head1, ListNode* head2){
while(head1 != head2){
head1 = head1->next;
head2 = head2->next;
}
return head1;
}
};

linked-list-cycle-ii leetcode C++的更多相关文章

  1. ★ Linked List Cycle II -- LeetCode

    证明单链表有环路: 本文所用的算法 能够 形象的比喻就是在操场其中跑步.速度快的会把速度慢的扣圈  能够证明,p2追赶上p1的时候.p1一定还没有走完一遍环路,p2也不会跨越p1多圈才追上  我们能够 ...

  2. Linked List Cycle II || LeetCode

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

  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 和I 通用算法和优化算法

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

  6. 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现

    引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ...

  7. LeetCode解题报告—— Linked List Cycle II & Reverse Words in a String & Fraction to Recurring Decimal

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

  8. [Leetcode Week6]Linked List Cycle II

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

  9. LeetCode 142. 环形链表 II(Linked List Cycle II)

    142. 环形链表 II 142. Linked List Cycle II 题目描述 给定一个链表,返回链表开始入环的第一个节点.如果链表无环,则返回 null. 为了表示给定链表中的环,我们使用整 ...

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

随机推荐

  1. [源码解析] 深度学习分布式训练框架 horovod (21) --- 之如何恢复训练

    [源码解析] 深度学习分布式训练框架 horovod (21) --- 之如何恢复训练 目录 [源码解析] 深度学习分布式训练框架 horovod (21) --- 之如何恢复训练 0x00 摘要 0 ...

  2. 中心对称数 II

    中心对称数 II 1.题目描述 中心对称数是指一个数字在旋转了 180 度之后看起来依旧相同的数字(或者上下颠倒地看). 找到所有长度为 n 的中心对称数. 示例 : 输入: n = 2 输出: [& ...

  3. HCNP Routing&Switching之路由控制、路由策略和IP-Prefix List

    前文我们了解了IS-IS路由聚合和认证相关话题,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/15306645.html:今天我们来聊一聊路由控制技术中的路由策 ...

  4. SpringAOP-动态代理,日志注入

    SpringAOP 前言: 1.AOP定义? 用来干啥的? 怎么用?(怎么跑通它的思路) 代理模式 为啥要学代理模式? -- 因为是SpringAop的底层 原有的代码不敢动,一动就是Bug,.所以使 ...

  5. PHP密码散列算法的学习

    不知道大家有没有看过 Laravel 的源码.在 Laravel 源码中,对于用户密码的加密,使用的是 password_hash() 这个函数.这个函数是属于 PHP 密码散列算法扩展中所包含的函数 ...

  6. centos7.5 SVN 搭建

    第一步:通过yum命令安装svnserve,命令如下: >yum -y install subversion 此命令会全自动安装svn服务器相关服务和依赖,安装完成会自动停止命令运行 若需查看s ...

  7. linux 上添加多个jdk

    1. 首先将你需要上传的jdk 上传并解压 2.你可以自定义解压的路径 3. alternatives --install /usr/bin/java java /usr/java/jdk1.7.0_ ...

  8. ES增删改查

    了解了一下python对es 7.5的操作,记录下,不难: #!/usr/bin/env python # -*- coding: UTF-8 -*- from settings import Con ...

  9. 鸿蒙内核源码分析(寄存器篇) | 小强乃宇宙最忙存储器 | 百篇博客分析OpenHarmony源码 | v38.02

    百篇博客系列篇.本篇为: v38.xx 鸿蒙内核源码分析(寄存器篇) | 小强乃宇宙最忙存储器 | 51.c.h .o 硬件架构相关篇为: v22.xx 鸿蒙内核源码分析(汇编基础篇) | CPU在哪 ...

  10. P5470-[NOI2019]序列【模拟费用流】

    正题 题目链接:https://www.luogu.com.cn/problem/P5470 题目大意 两个长度为\(n\)的序列\(a,b\),求出它们两个长度为\(K\)的子序列,且这两个子序列至 ...