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?

解题思路

设链表长度为n,头结点与循环节点之间的长度为k。定义两个指针slow和fast,slow每次走一步,fast每次走两步。当两个指针相遇时,有:

  • fast = slow * 2
  • fast - slow = (n - k)的倍数
    由上述两个式子能够得到slow为(n-k)的倍数

两个指针相遇后,slow指针回到头结点的位置,fast指针保持在相遇的节点。此时它们距离循环节点的距离都为k,然后以步长为1遍历链表,再次相遇点即为循环节点的位置。

实现代码

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

转载于:https://www.cnblogs.com/blfbuaa/p/7049933.html

[LeetCode] Linked List Cycle II相关推荐

  1. leetcode: Linked List Cycle II

    http://oj.leetcode.com/problems/linked-list-cycle-ii/ Given a linked list, return the node where the ...

  2. LeetCode Linked List Cycle II(floyd cycle)

    问题:给出一个链表,可能存在环,要求输出环的起点位置 思路:使用Floyd cycle algorithm 具体代码参考: https://github.com/wuli2496/OJ/tree/ma ...

  3. LeetCode Linked List Cycle II

    题意:给出一个链表,判断是否有环,如果有环,输出环的起点,如果没有,输出null 思路:从起点开始,一个每次走一步,一个每次走两步,如果两点出现重合,说明有环,此时,一个从起点开始,另一个从重合点开始 ...

  4. Leetcode 142. Linked List Cycle II

    地址:Leetcode 142. linked list Cycle II 问题描述:检测链表是否存在环,是的话返回环入口,否则返回None. 这道题有两个思路,一个是经典的快慢指针的思路,另外一个是 ...

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

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

  6. 【To Do】LeetCode 142. Linked List Cycle II

    LeetCode 142. Linked List Cycle II Solution1:我的答案 这道题多次遇到,牢记此解法 这道题要深思一下,快指针和慢指针的速度对比不同,会产生什么不同的结果? ...

  7. leetcode day5 -- Reorder List Linked List Cycle II

    1.  Reorder List Given a singly linked list L: L0→L1→-→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln ...

  8. LeetCode141 Linked List Cycle. LeetCode142 Linked List Cycle II

    链表相关题 141. Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up: Can ...

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

最新文章

  1. datagrid分页问题(前后跳页)《控件版》
  2. 生产者与消费者-1:N-基于list
  3. xps文档打印后winform界面文字丢失
  4. getitemdata中参数设置
  5. 构建高可靠hadoop集群之0-hadoop用户向导
  6. Re:从 0 开始的微服务架构--(三)微服务架构 API 的开发与治理--转
  7. 约数研究pascal程序
  8. 带你理清:ROS机器人导航功能实现、解析、以及参数说明
  9. 【蜻蜓Dragonfly】client部署
  10. GlobalMapper导出S57水深数据
  11. JAVA正反合——原码、补码与反码学习笔记’
  12. 激活数字经济澎湃动能
  13. CVPR 2019视频目标跟踪算法Pipeline集合
  14. 检查手机端还是非手机端,做m站和pc站的适配
  15. 如何用纯CSS将图片填满div,自适应容器大小
  16. 使用WINDOWS消息使火绒窗口关闭
  17. 转正后的一点工作体会
  18. Qt 集成miniblink浏览器库之4 解决兼容性问题
  19. 京杭论坛-----华东顾小清老师---数据赋能教育技术学科进入AIED时代(5月7日)
  20. rac 火星舱如何备份oracle_Oracle RAC OCR 和 VotingDisk 的备份与恢复

热门文章

  1. Chrome 技术篇-常用web调试技巧清除缓存并硬性重新加载
  2. Python+selenium 自动化-chrome驱动的下载安装
  3. VSCode搭建ARM(STM32开发环境)
  4. 可视化卷及神经网络热力图
  5. windows下mysql安装失败的一个解决案例
  6. ArrayList中remove方法和set(null)的区别
  7. 离散化+unique()+二分查找
  8. centos下配置gitosis服务器
  9. iOS App 上架(Analysis 工具使用)
  10. CSS3---选择器