题目:

Given a linked list, determine if it has a cycle in it.

思路:

对于判断链表是否有环,方法很简单,用两个指针,一开始都指向头结点,一个是快指针,一次走两步,一个是慢指针,一次只走一步,当两个指针重合时表示存在环了。

fast先进入环,在slow进入之后,如果把slow看作在前面,fast在后面每次循环都向slow靠近1,所以一定会相遇,而不会出现fast直接跳过slow的情况。

/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/ /**
* @param {ListNode} head
* @return {boolean}
*/
var hasCycle = function(head) {
if(head==null||head.next==null){
return false;
} var s=head,f=head.next.next;
while(s!=f){
if(f==null||f.next==null){
return false;
}else{
s=s.next;
f=f.next.next;
}
} return true;
};

【链表】Linked List Cycle的更多相关文章

  1. LeetCode 141. 环形链表(Linked List Cycle) 19

    141. 环形链表 141. Linked List Cycle 题目描述 给定一个链表,判断链表中是否有环. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 ...

  2. [Java]LeetCode141. 环形链表 | Linked List Cycle

    Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...

  3. 带环链表 linked list cycle

    1 [抄题]: 给定一个链表,判断它是否有环. [思维问题]: 反而不知道没有环怎么写了:快指针fast(奇数个元素)或fast.next(偶数个元素) == null [一句话思路]: 快指针走2步 ...

  4. LeetCode 141:环形链表 Linked List Cycle

    给定一个链表,判断链表中是否有环. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始). 如果 pos 是 -1,则在该链表中没有环. Given a l ...

  5. [LeetCode] Linked List Cycle II 单链表中的环之二

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  6. [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 ...

  7. [算法][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 ...

  8. [CareerCup] 2.6 Linked List Cycle 单链表中的环

    2.6 Given a circular linked list, implement an algorithm which returns the node at the beginning of ...

  9. 【题解】【链表】【Leetcode】Linked List Cycle II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

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

随机推荐

  1. IP之ALTDDIO_in仿真

    需要添加altera_mf库,才可以仿真. 上升沿输出,把前一个时钟的数据输出来. `timescale 1 ns/ 1 ns; module altddio_in_ip_tb; reg rst; r ...

  2. Windows命令行参数(不断更新)

    这里先讲一下系统变量: 注意:一旦将路径加入到环境变量Path中,那么运行它下面的程序的时候就不用非得指定到目标路径中,直接键入命令就行了. 1.type命令:打开并读取文件里面的内容. C:\Use ...

  3. git图解:代码区域总结

    本文背景,在实际项目中使用git已有一年多,发现不少同事虽然会使用常用git指令,但并不理解每个指令对应的作用原理.今天静下心总结下git 的基本理解:代码的存在区域:本文以实际项目出发,理清使用gi ...

  4. (字符串处理)Fang Fang -- hdu -- 5455 (2015 ACM/ICPC Asia Regional Shenyang Online)

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=5455 Fang Fang Time Limit: 1500/1000 MS (Java/Others)  ...

  5. B样条参数曲线学习(1)

    B样条参数曲线学习 Bezier曲线有许多优越性,但有两点不足: (1) 特征多边形的顶点个数决定了Bezier曲线的阶次,并且在阶次较大时,特征多边形对曲线的控制将会减弱: (2) Bezier曲线 ...

  6. java:从消息机制谈到观察者模式

    从简单的例子开始 同样,我们还是先看一个简单例子:创建一个窗口实现加法的计算功能.其效果如下: 图1: 加法计算 Calculator.java: import javax.swing.*; impo ...

  7. 1. Two Sum [Array] [Easy]

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  8. Java动态绑定与静态绑定

    Java动态绑定来自于继承体现,子类继承父类,子类重新覆盖了父类的方法,就是动态绑定,以下举例: (动态绑定是在运行期间) 动物类: /** * 创建一个动物类 * @author Liudeli * ...

  9. java 反射应用

    场景需求最近的一次解析数据包中,因为协议有改变,本来的定长的包,现在变为不定长的.举个例子,本来协议中规定,一个包中,有8个标签,但是每次上来的,不一定都有8个,没有的话,硬件过来的都是0.同时里面也 ...

  10. go基本操作

    看了一段时间的go的知识了,本来是冲着它是系统级的语言去的,同时又有java的的样子.看了这么久,发现这语言挺好的,语法精简,有c的遗传.在面向对象上,也有些许的java风格.写web的时候,这风格和 ...