题目意思:链表有环,返回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. ♫【异步】短小强悍的JavaScript异步调用库

    短小强悍的JavaScript异步调用库 var queue = function(funcs, scope) { (function next() { if(funcs.length > 0) ...

  2. 【转】Android自定义Adapter的ListView的思路及代码

    原文网址:http://www.jb51.net/article/37236.htm Android自定义Adapter的ListView的思路及代码,需要的朋友可以参考一下   在开发中,我们经常使 ...

  3. SPJ 讨论

    设有一个SPJ数据库,包括S,P,J,SPJ四个关系模式: S( SNO,SNAME,STATUS,CITY): P(PNO,PNAME,COLOR,WEIGHT): J(JNO,JNAME,CITY ...

  4. 使用Array

    public class UsingArray {     public static void output(int[]Array)     {         if(Array!=null)    ...

  5. [Audio processing] wav音频文件合并

    合并多个文件,需要包含1.文件读取和写入功能,2.数组合并 package com.audioprocessingbox.myfunc; import java.io.File; import jav ...

  6. The game of life(生命游戏)新算法

    我写了一种常见的实现算法,和另一种新算法,即不是每次循环计算每个细胞的周围细胞数来产生下一时刻,而是每次每个产生状态变化的细胞主动通知周围的邻居,因此每个细胞增加一个用来记录邻居数的字段.由邻居数决定 ...

  7. lightoj 1013 dp

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1013 #include <cstdio> #include <cst ...

  8. hdoj 1513 Palindrome【LCS+滚动数组】

    Palindrome Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  9. C#实现文件数据库

    本文转载:http://www.cnblogs.com/gaochundong/archive/2013/04/24/csharp_file_database.html#commentform 本文为 ...

  10. JS 操作 HTML 自定义属性

    <input type="text" id="txtBox" displayName="123456" /> 获取自定义属性值: ...