LeetCode -- Linked List Circle ii
Question:
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
Analysis:
只想到了,首先判断是否有环,若有环,则总链首开始,一次判断是否是环的开始,这样T(O) = O(n^2)。
其实是一个数学问题,详细思路参照链接http://blog.csdn.net/sbitswc/article/details/27584037 。
Answer:
public ListNode detectCycle(ListNode head) {
ListNode fast = head, slow = head;
while(fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
if(fast == slow)
break;
}
if(fast == null || fast.next == null)
return null;
slow = head;
while(fast != slow) {
fast = fast.next;
slow = slow.next;
}
return fast;
}
LeetCode -- Linked List Circle ii的更多相关文章
- 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 ...
- LeetCode: Linked List Cycle II 解题报告
Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...
- [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]Linked List Cycle II解法学习
问题描述如下: Given a linked list, return the node where the cycle begins. If there is no cycle, return nu ...
- 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 Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- [Leetcode] Linked list cycle ii 判断链表是否有环
Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull. Follo ...
- [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] Linked List Cycle II, Solution
Question : Given a linked list, return the node where the cycle begins. If there is no cycle, return ...
随机推荐
- shell 输出带颜色字体
输出特效格式控制:\033[0m 关闭所有属性 \033[1m 设置高亮度 \03[4m 下划线 \033[5m 闪烁 \033[7m 反显 \033[8m 消隐 \ ...
- Linux环境下tomcat如何热部署
1.修改tomcat配置文件 1.1第一步修改tomcat-users.xml <role rolename="manager-gui" /> <role rol ...
- js实现区县联动
1. 引入区县联动函数如下,将provinceList中数据改为需要联动的数据信息 var addressInit = function(_cmbProvince, _cmbCity, _cmbAre ...
- 吐血分享:QQ群霸屏技术教程(利润篇)
QQ群技术,不论日进几百,空隙时间多的可以尝试,日进100问题不大. QQ群技术,如何赚钱,能赚多少钱?不同行业,不同关键词,不同力度,不一样的产出. 群费 群费,这个和付费群是有区别的,群费在手机端 ...
- 深入理解is_callable和method_exists
一.函数解析 is_callable() 定义: (PHP 4 >= 4.0.6, PHP 5, PHP 7) is_callable — 检测参数是否为合法的可调用结构 bool is_cal ...
- 675. Cut Off Trees for Golf Event
// Potential improvements: // 1. we can use vector<int> { h, x, y } to replace Element, sortin ...
- Redis缓存数据库的安装与配置(3)
3 Redis主动同步设置方法 Redis主从同步 1.Redis主从同步特点 一个master可以拥有多个slave 多个slave可以连接同一个master,还可以连接到其他slave 主从复制不 ...
- node获取头信息数据
req.fresh req.stale var version = 100; app.get('/test',function(req,res){ res.set('etag',version); i ...
- PHP.41-TP框架商城应用实例-后台16-商品属性2-AJAX添加、删除
添加商品属性 思路:根据[后台15]类型表{id,type_name}与属性表{id,attr_name,attr_type,attr_option_values,type_id} 1.建表商品属性 ...
- List中的FindAll用法
在泛型List中查找符合某个字段的全部数据,可以采用如下方式: //1.现将实体数据listList<ADDaAn> objDAList = db.ADDaAns.ToList(); // ...