Linked List Cycle
Given a linked list, determine if it has a cycle in it.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
bool hasCycle(ListNode *head) {
if(!head)
return false;
ListNode* fast = head->next;
ListNode* slow = head;
while(fast) {
if(fast == slow)
return true;
if(fast->next&&fast->next->next){//短路的技巧
fast = fast->next->next;
slow = slow->next;
}
else
return false;
}
return false;
}
};
设置两个指针,一个快指针,一个慢指针。快指针每次走两步,慢指针每次走一步,如果相遇就是有环。
19行先判断快指针能不能走1步,如果能在判断2步之后是不是链表末尾,如果不是末尾就可以向下走。
==============================我是分割线=============================================
《编程之美》上看到一道题目,判断两个两个链表(无环)是否相交,可以转化为这题的解法,把第二个链表头接到另一个的末尾,在检测是否有环,有环就是相交。
两一种解法更简单,比较倒数第二节链表是否相等。越来愈感受到算法的美妙了.QAQ
Linked List Cycle的更多相关文章
- [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 单链表中的环
Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using ex ...
- [LintCode] Linked List Cycle 单链表中的环
Given a linked list, determine if it has a cycle in it. ExampleGiven -21->10->4->5, tail co ...
- [算法][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 ...
- LEETCODE —— Linked List Cycle [Floyd's cycle-finding algorithm]
Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it ...
- 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 ...
- [CareerCup] 2.6 Linked List Cycle 单链表中的环
2.6 Given a circular linked list, implement an algorithm which returns the node at the beginning of ...
- 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 II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- 3月2日 Linked List Cycle
今天星期天,准备好周一的PPT内容,再回来做题,以后考虑周末做一个APP或者微信帐号玩吧. 回到题目, Linked List Cycle,一个检查单项链表是否有环路的问题. 题目周五的时候就简单做过 ...
随机推荐
- java内部类的使用
内部类不是很好理解,但说白了其实也就是一个类中还包含着另外一个类 如同一个人是由大脑.肢体.器官等身体结果组成,而内部类相当于其中的某个器官之一,例如心脏:它也有自己的属性和行为(血液.跳动) 显然, ...
- sencha/extjs 动态创建grid表格
//创建普通表格 id,父容器,标题,json数据字符串,列名(逗号分隔),json数据key即store的fields属性(逗号分隔) function createCommonTable(id, ...
- .net reflector激活
1.断网 2. 运行.NET Reflector,点击Help -> Activate 3. 运行注册机,复制注册机生成的序列号,粘贴到.NET Reflector中的激活输入框 4. 点击激活 ...
- 造完美的go开发环境
http://www.cnblogs.com/qgymje/p/3912259.html 这篇原来是给公司里使用go开发的交流用的,主要是工具的安装,用markdown写的,发布了内部gitlab ...
- java注释指导手册
译文出处: Toien Liu 原文出处:Dani Buiza 编者的话:注解是java的一个主要特性且每个java开发者都应该知道如何使用它. 我们已经在Java Code Geeks提供了丰富 ...
- 利用javascript、php和ajax实现计算器
计算器和ajax部分: <?php /** * Created by PhpStorm. * User: Administrator * Date: 16-9-2 * Time: 上午9:20 ...
- linux shell 编程
1,获取命令执行的结果,字符串拼接(脚本最常使用的功能) cmd_result=$(date +%Y%b%d) //使用变量获取命令执行的结果 或者 cmd_result=`date ...
- PHP imagecopyresampled 参数图示
- 【JWPlayer】官方JWPlayer去水印步骤
在前端播放视频,现在用html5的video标签已经是一个不错的选择,不过有时候还是需要用StrobeMediaPlayback.JWPlayer这一类的flash播放器,JWPlayer的免费版本带 ...
- Apache服务器的URL重定向
前端时间要整个Apache重定向功能,在此记录一下. 一.安装Apache Windows版本官方下载安装文件httpd-2.2.21-win32-x86-openssl-0.9.8r,选择安装目录, ...