题目意思:链表有环,返回true,否则返回false

思路:两个指针,一快一慢,能相遇则有环,为空了没环

  ps:很多链表的题目:都可以采用这种思路

 /**
* 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==NULL)return false;
ListNode *p1,*p2;
p1=p2=head;
while(p2->next&&p2->next->next){ //要判断p->next->next为空先要判断p->next是否为空,以免产生p->next->next不存在的蛋疼问题
p1=p1->next;
p2=p2->next->next;
if(p1==p2)
return true;
}
return false;
}
};

141 Linked List Cycle(判断链表是否有环Medium)的更多相关文章

  1. LeetCode 141. Linked List Cycle 判断链表是否有环 C++/Java

    Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked lis ...

  2. 141. Linked List Cycle(判断链表是否有环)

    141. Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you sol ...

  3. LeetCode 141. Linked List Cycle(判断链表是否有环)

    题意:判断链表是否有环. 分析:快慢指针. /** * Definition for singly-linked list. * struct ListNode { * int val; * List ...

  4. [leetcode]141. Linked List Cycle判断链表是否有环

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

  5. [Leetcode] Linked list cycle 判断链表是否有环

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

  6. [LeetCode] 141. Linked List Cycle 单链表中的环

    Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked lis ...

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

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

  8. [LeetCode] 142. Linked List Cycle II 链表中的环 II

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

  9. LeetCode 141. Linked List Cycle环形链表 (C++)

    题目: Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked ...

随机推荐

  1. 【转】Java 字符串常用操作(String类)

    原文网址:http://www.cnblogs.com/freeabyss/archive/2013/05/15/3187057.html 字符串查找 String提供了两种查找字符串的方法,即ind ...

  2. Spring 配置中的 default-lazy-init属性

    spring的容器是提供了lazy-load的,即默认的缺省设置是bean没有lazy-load,该属性处于false状态,这样导致spring在启动过程导致在启动时候,会默认加载整个对象实例图,从初 ...

  3. awr报告

    BEGIN DBMS_WORKLOAD_REPOSITORY.CREATE_SNAPSHOT();END;/ exec DBMS_WORKLOAD_REPOSITORY.CREATE_SNAPSHOT ...

  4. Hibernate详解(5)——Hibernate核心接口和工作原理

    Hibernate核心接口 Hibernate有五大核心接口,分别是:Session Transaction Query SessionFactoryConfiguration .这五个接口构成了Hi ...

  5. iframe页面改动parent页面的隐藏input部件value值,不能触发change事件。

    实现一个依据iframe页面返回充值卡类型不同,安排不同的input部件. 点击选择弹出一个iframe.点击充值卡数据行.返回1.充值卡类型.2.充值卡id(用的UUID).3.充值卡号(字符串). ...

  6. 自我理解foreach工作原理

        很多时候我们在使用for循环遍历一个数组的时候,我们都知道可以通过下标的索引找到当前数组中所对应的数据.这只对于简单的数组或集合,如果我们存储的数据不止只有数据项,还有一个标识项,就如同Has ...

  7. ubuntu开机黑屏

    以下皆在VM虚拟机下 (1)ctr+alt+f4 进入命令行 (2) sudo apt-get update sudo apt-get install xserver-xorg-lts-quantal ...

  8. [转] Linux文件系统之hard link&symbol link

    这个图很清楚的表示出硬链接和软链接的方式. 1.硬链接: 基本定义:硬链接是有着相同inode号的仅文件名不同的文件(该文件名包含路径信息). 理解:如图,hard link和原始file通过同一个i ...

  9. Android内存等信息

    1. Linux中proc目录下文件详解 http://wenku.baidu.com/view/2ce89f00a6c30c2259019ef1.html 2. Android系统/proc目录详解 ...

  10. TCP/IP协议原理与应用笔记09:数据通信---封装

    2016-08-091. 数据通信----封装: 2. 协议数据单元: PDU:对等层数据通信的单元. 比如Source端的应用层 和 Destination端的应用层是对等层(L7),这个时候L7 ...