题目意思:链表有环,返回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. c#反序列化

    C#序列化(转载) 2011-03-15  |  凯之风  |  转藏(2) 序列化有下列要求: A> 只序列化PUBLIC的成员变量和属性 B> 类必须有默认识构造函数 C> 如果 ...

  2. bzoj1588 [HNOI2002]营业额统计(Treap)

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 11485  Solved: 4062[Submit][Sta ...

  3. docs

    https://www.eucalyptus.com/docs/eucalyptus/3.4/index.html  [Eucalyptus PDF官方下载] http://aws.amazon.co ...

  4. SQL随笔

    多表分页查询: ,; 向表中插入新的字段: ALTER TABLE `Table_name` ) NOT NULL DEFAULT '' AFTER `id`;  更新表数据: UPDATE [LOW ...

  5. git如何clone所有的远程分支

    问题: 文/赖忠标 周末在家里改了下代码,新建了个angular版本的分支,然后push到coding.net上面了. 今天,到公司却不知道怎么拉取这个angular分支到公司的电脑上面.如下图(1) ...

  6. Unity3d个人信息开发流程

    1.首先先对需要交互的属性进行G/S,比如声明金币的属性 private int _coin; public String Coin{ get{ return _coin; } set{ return ...

  7. mongo 多条件 查询

    var query1 = Query<BaseManagerForCompanyModel>.EQ(q => q.sGuidBaseCompany, sGuidBaseCompany ...

  8. MySQL更新时Error Code:1093和Error Code:1175的解决办法

    Error Code: 1093. You can't specify target table 'ws_product' for update in FROM clause 这个是我们在使用upda ...

  9. 最近新出的C++右值引用的意思

    看了一下这种方法的介绍,个人感觉,右值引用,更像人类的思想了,有些将编译前与编译后结合紧密的感觉. 左值引用是变量名的别名,右值引用是值的别名,也就是不将值赋给一个变量名所在的地址,直接将值所在的初始 ...

  10. [PWA] 12. Intro to IndexedDB

    Use the library indexedDB-promised. Create a database and stroe: import idb from 'idb'; // Open(db_n ...