Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null
.
Note: Do not modify the linked list.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
ListNode* slow = head;
ListNode* fast = head;
while(fast && fast->next){
slow = slow->next;
fast = fast->next->next;
if(slow == fast){
ListNode* slow2 = head;
while(slow2 != slow){
slow = slow->next;
slow2 = slow2->next;
}
return slow;
}
}
return nullptr;
}
};
Linked List Cycle II的更多相关文章
- [算法][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 ...
- 15. 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 solve i ...
- 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 ...
- LeetCode解题报告:Linked List Cycle && Linked List Cycle II
LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...
- 【LeetCode练习题】Linked List Cycle II
Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it ...
- [Linked List]Linked List Cycle,Linked List Cycle II
一.Linked List Cycle Total Accepted: 85115 Total Submissions: 232388 Difficulty: Medium Given a linke ...
- Linked List Cycle && Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note ...
- LeetCode之“链表”:Linked List Cycle && Linked List Cycle II
1.Linked List Cycle 题目链接 题目要求: Given a linked list, determine if it has a cycle in it. Follow up: Ca ...
- 141. Linked List Cycle&142. Linked List Cycle II(剑指Offer-链表中环的入口节点)
题目: 141.Given a linked list, determine if it has a cycle in it. 142.Given a linked list, return the ...
- 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 ...
随机推荐
- 小记:获取系统时间的long值,格式化成可读时间。
/** * 返回的字符串形式是形如:2013年10月20日 20:58 * */ public static String formatTimeInMillis(long timeInMillis) ...
- Validform自定义提示效果-使用自定义弹出框
$(function(){ $.Tipmsg.r=null; $("#add").Validform({ tiptype:function(msg){ layer.msg(msg) ...
- KStar ----BPM应用框架,K2 的新星
“KStar”是基于K2 BPM搭建的应用框架产品,将K2最佳实践方案以产品的形式呈现给用户,该框架面向SOA服务,便于二次开发和扩展,流程设计.用户组织.业务表单.流程管理.系统集成等开发工作,都按 ...
- 企业需要k2来解放孤岛危机
当我谈孤岛危机时,我谈些什么?你以为我要说的是一款风靡的游戏?那恐怕要让你失望了,今天要谈的是“企业管理体系孤岛”,但更多人甚至都没意识到这是危机. 下面的场景,也许你会觉得似曾相识. 场景一 某制鞋 ...
- Spring学习笔记之Bean的实例化
一.bean的实例化方法有3种, 1.构造器实例化 2.静态工厂方法实例化 3.实例工厂方法实例化 二.用构造器来实例化 <bean id="ShunDao" class=& ...
- Cisco IOS Software Activation Command Reference
clear license agent : to clear license agent statistics counters or connection statistics (in privil ...
- dialog参数、方法以及事件
参数(options) DOM方式初始化dialog的,推荐使用集合属性data-options定义参数,如果使用data属性定义参数,注意转换成对应的名称. 名称 类型 默认值 描述 id stri ...
- iis提示“另一个程序正在使用此文件,进程无法访问。(异常来自HRESULT:0x80070020)
看看IIS的网站,惊人的发现default web site是停止状态.印象中没有停止它啊.右键->管理网站->启动.点击启动后居然弹出:“另一个程序正在使用此文件,进程无法访问.(异常来 ...
- 在MongoDB中实现聚合函数 (转)
随着组织产生的数据爆炸性增长,从GB到TB,从TB到PB,传统的数据库已经无法通过垂直扩展来管理如此之大数据.传统方法存储和处理数据的成本将会随着数据量增长而显著增加.这使得很多组织都在寻找一种经济的 ...
- Html获取经纬度
if (navigator.geolocation) { navigator.geolocation.getCurrentPosition( function success(pos) {alert( ...