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

To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexed) in the linked list where tail connects to. If pos is -1, then there is no cycle in the linked list.

Note: Do not modify the linked list.

Example 1:

Input: head = [3,2,0,-4], pos = 1
Output: tail connects to node index 1
Explanation: There is a cycle in the linked list, where tail connects to the second node.

Example 2:

Input: head = [1,2], pos = 0
Output: tail connects to node index 0
Explanation: There is a cycle in the linked list, where tail connects to the first node.

Example 3:

Input: head = [1], pos = -1
Output: no cycle
Explanation: There is no cycle in the linked list.

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

Solution:

  使用快慢指针,若快慢指针能重合上,那就有环

  然后快指针从头移动,与此同时慢指针一起先后移动,当两个指针再次重合时,则就是环的入口!

 class Solution {
public:
ListNode *detectCycle(ListNode *head) {
if (head == nullptr || head->next == nullptr)return nullptr;
ListNode *slow, *fast;
slow = fast = head;
while (fast && fast->next)
{
slow = slow->next;
fast = fast->next->next;
if (fast == slow)
break;
}
if (fast != slow)return nullptr;
slow = head;
while (slow != fast)
{
slow = slow->next;
fast = fast->next;
}
return fast;
}
};

力扣算法——142LinkedListCycleII的更多相关文章

  1. 力扣算法经典第一题——两数之和(Java两种方式实现)

    一.题目 难度:简单 给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数, 并返回它们的数组下标. 你可以假设每种输入只会对应一 ...

  2. 力扣算法题—069x的平方根

    实现 int sqrt(int x) 函数. 计算并返回 x 的平方根,其中 x 是非负整数. 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去. 示例 1: 输入: 4 输出: 2 示例 ...

  3. C++双指针滑动和利用Vector实现无重复字符的最长子串—力扣算法

    题目: 力扣原题链接:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/ 给定一个字符串, ...

  4. 力扣算法——135Candy【H】

    老师想给孩子们分发糖果,有 N 个孩子站成了一条直线,老师会根据每个孩子的表现,预先给他们评分. 你需要按照以下要求,帮助老师给这些孩子分发糖果: 每个孩子至少分配到 1 个糖果.相邻的孩子中,评分高 ...

  5. 力扣算法题—060第K个排列

    给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列. 按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下: "123" "132&qu ...

  6. 力扣算法题—050计算pow(x, n)

    #include "000库函数.h" //使用折半算法 牛逼算法 class Solution { public: double myPow(double x, int n) { ...

  7. 62 (OC)* leetCode 力扣 算法

    1:两数之和 1:两层for循环 2:链表的方式 视频解析 2:两数相加 两数相加 3. 无重复字符的最长子串 给定一个字符串,请找出其中长度最长且不含有重复字符的子串,计算该子串长度 无重复字符的最 ...

  8. 力扣算法题—147Insertion_Sort_List

    Sort a linked list using insertion sort. A graphical example of insertion sort. The partial sorted l ...

  9. 力扣算法:125-验证回文串,131-分割回文串---js

    LC 125-验证回文串 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. 注:回文串是正着读和反着读都一样的字符串. ...

随机推荐

  1. OpenCV2.4.8 + CUDA7.5 + VS2013 配置

    配置过程主要参考:https://initialneil.wordpress.com/2014/09/25/opencv-2-4-9-cuda-6-5-visual-studio-2013/ 1.为什 ...

  2. chromedriver安装报错

     解决方法:   可以使用 npm init -f命令生成package.json,package.json中缺少的字段可以参照模板 package.json进行填充,package.json中的字段 ...

  3. PAT_A1071#Speech Patterns

    Source: PAT A1071 Speech Patterns (25 分) Description: People often have a preference among synonyms ...

  4. 正则split的使用

    import re #\s 空格 +号表示至少出现一次 # path = 'C:\\Users\\cz\Desktop\py06\\PY6_Day01\\爬虫作业\\2018_03_07\\05_sp ...

  5. ss证书问题

    #SSL--校验网站证书 #一.什么是SSL证书 from urllib import request #ssl免验证 import ssl ssl._create_default_https_con ...

  6. 零基础如何快速学习好Python网络爬虫?

    Python网络爬虫上手很快,能够尽早入门,可是想精通确实是需求些时间,需求达到爬虫工程师的级别更是需求煞费苦心了,接下来共享的学习道路是针对小白或许学习Python网络爬虫不久的同伴们. 学习网络爬 ...

  7. ida吧

    经过IDA反编译后的代码是:int __cdecl Ompress(void *Dst, int a2, int a3, int a4)//dst( [esp+24h][ebp+4h] );a2([e ...

  8. mysql的几种锁

    由于对于mysql的锁机制了解的并不深入,所以翻阅了资料,整理一下自己所理解的锁.以mysql数据库的InnoDB引擎为例,因为InnoDB支持事务.行锁.表锁:且现在大部分公司使用的都是InnoDB ...

  9. elasticsearch 6.x 安装search guard

    前言 es之前版本一直无用户验证功能,不过官方有提供一x-pack,但是问题是付费.在es的6.3.2版本中,已经集成了x-pack,虽然es团队已经对x-pack开源,但是在该版本中如果需要使用到安 ...

  10. MQ总结

    对于ActiveMQ:JMS可靠消息机制.JMS发布订阅与点对点通讯.ActiveMQ高可用集群解决方案.ActiveMQ补偿与重试策略.ActiveMQ整合SpringBoot Kafka:整合Zo ...