LeetCode Linked List Cycle II
题意:给出一个链表,判断是否有环,如果有环,输出环的起点,如果没有,输出null
思路:从起点开始,一个每次走一步,一个每次走两步,如果两点出现重合,说明有环,此时,一个从起点开始,另一个从重合点开始,两个每次走一次,此时重合点就是环的起点
代码如下:
class Solution {public ListNode detectCycle(ListNode head) {ListNode slow = head;if (null == head) return null;ListNode fast = head;do {if (null == fast || null == slow) return null;slow = slow.next;if (null == fast.next) return null;fast = fast.next.next;}while (fast != slow);slow = head;while (fast != slow){fast = fast.next;slow = slow.next;}return slow;}
}
LeetCode Linked List Cycle II相关推荐
- leetcode: Linked List Cycle II
http://oj.leetcode.com/problems/linked-list-cycle-ii/ Given a linked list, return the node where the ...
- LeetCode Linked List Cycle II(floyd cycle)
问题:给出一个链表,可能存在环,要求输出环的起点位置 思路:使用Floyd cycle algorithm 具体代码参考: https://github.com/wuli2496/OJ/tree/ma ...
- [LeetCode] Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- Leetcode 142. Linked List Cycle II
地址:Leetcode 142. linked list Cycle II 问题描述:检测链表是否存在环,是的话返回环入口,否则返回None. 这道题有两个思路,一个是经典的快慢指针的思路,另外一个是 ...
- LeetCode 142. 环形链表 II(Linked List Cycle II)
142. 环形链表 II 142. Linked List Cycle II 题目描述 给定一个链表,返回链表开始入环的第一个节点.如果链表无环,则返回 null. 为了表示给定链表中的环,我们使用整 ...
- 【To Do】LeetCode 142. Linked List Cycle II
LeetCode 142. Linked List Cycle II Solution1:我的答案 这道题多次遇到,牢记此解法 这道题要深思一下,快指针和慢指针的速度对比不同,会产生什么不同的结果? ...
- 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 ...
- 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 ...
- 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 ...
最新文章
- decimal类型保留两位小数
- c语言中有关指针的选择题,关于C语言指针的几道选择题能选几道就几道吧万分感谢1. 若已定义 爱问知识人...
- Exchange 2016 先决条件
- k-means聚类分割
- 初学者浅谈oracle中的一些sql
- layui表单验证提交
- 计算机网络实验二:网络基础编程实验
- 缓动函数及DOTWeen的SetEase方法
- python 函数进阶_python之函数进阶
- 如何快速实现数组/字符串的逆序(用reverse函数实现)
- DANN:Unsupervised Domain Adaptation by Backpropagation
- Kotlin 代码生成 之 kotlinpoet 使用
- 常见计算机系统哪些为GUI,GUI 是什么
- 一款好用的网络骗子举报系统无加密版本源码
- 微信开发实例视频教程-深入浅出微信公众平台实战开发
- 关于Flask通过局域网访问(局域网内任意设备通过ip:port访问)
- 网络分流器|网络分流器|100G分流器不仅仅是带宽升级
- IntelliJ IDEA创建Servlet最新方法 Idea版本2020.2.2以及IntelliJ IDEA创建Servlet 404问题(超详细)
- 盘点2017年银行存管口碑平台:微贷网、财佰通、融金所等
- FLYMCU 烧写STM32注意
热门文章
- 2016 - 1 - 19NSOpertation的依赖关系和监听
- linux系统中用户切换
- Asp.net 序列化应用实例(转载)
- android gc 回调,android - 使用Picasso通过Bitmap获取回调
- 剁馅机器人图片_黄金手撕面包培训图片信得过的工艺利润高
- python里面的之前打过的记忆信息-忘了Python关键语句?这份备忘录拯救你的记忆...
- 精通python爬虫框架-精通Python爬虫从Scrapy到移动应用(文末福利)
- python手机版下载3.7.2-Python3.5.2
- 零基础python入门书籍-零基础如何学好python?推荐6本入门书籍,帮你打基础
- python从入门到精通怎么样-Python 从入门到精通:一个月就够了