【题目】

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

Follow up:

Can you solve it without using extra space?

【题意】

推断一个单向链表是否有环

【思路】

维护两个指针p1和p2,p1每次向前移动一步,p2每次向前移动两步

    假设p2可以追上p1,则说明链表中存在环

【代码】

/**
* 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) {
ListNode*p1=head;
ListNode*p2=head;
while(p2){
//p1向前走一步
p1=p1->next;
//p2向前走两部
p2=p2->next;
if(p2)p2=p2->next;
//推断p2是否追上了p1
if(p2 && p2==p1)return true;
}
return false;
}
};

LeetCode: Linked List Cycle [141]的更多相关文章

  1. LeetCode Linked List Cycle II 和I 通用算法和优化算法

    Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...

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

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

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

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

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

  6. LeetCode: Linked List Cycle II 解题报告

    Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...

  7. LeetCode: Linked List Cycle 解题报告

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

  8. LeetCode Linked List Cycle 解答程序

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

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

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

随机推荐

  1. [NOIP2016]天天爱跑步(树上差分+线段树合并)

    将每个人跑步的路径拆分成x->lca,lca->y两条路径分别考虑: 对于在点i的观察点,这个人(s->t)能被观察到的充要条件为: 1.直向上的路径:w[i]=dep[s]-dep ...

  2. Educational Codeforces Round 41 (Rated for Div. 2) ABCDEF

    最近打的比较少...就只有这么点题解了. A. Tetris time limit per test 1 second memory limit per test 256 megabytes inpu ...

  3. 【DFS】STAMPS

    [Poj1010]STAMPS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18867   Accepted: 5469 ...

  4. BZOJ 1042:[HAOI2008]硬币购物(容斥原理+DP)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1042 [题目大意] 硬币购物一共有4种硬币.面值分别为c1,c2,c3,c4. 某人去 ...

  5. 【最大流】BZOJ1305-[CQOI2009]dance跳舞

    [题目大意] 一次舞会有n个男孩和n个女孩.每首曲子开始时,所有男孩和女孩恰好配成n对跳交谊舞.每个男孩都不会和同一个女孩跳两首(或更多)舞曲.有一些男孩女孩相互喜欢,而其他相互不喜欢(不会“单向喜欢 ...

  6. 微信小程序 Session 失效

    微信小程序 Session 失效 微信小程序,前端请求后端,中间多了个微信服务器,所以请求的流程就是 页面--微信服务器--目标服务器 这就导致了一个问题 session 每次请求都是一个新的会话 解 ...

  7. Activity(活动)生命周期--项目测试

    一.新建一个Activitylifetest项目,允许Android Studio帮我们自动创建活动和布局,并且勾选Luancher Activity来将创建的活动设置为主活动. 可以参考: http ...

  8. UVA 10163 - Storage Keepers(dp)

    本文出自   http://blog.csdn.net/shuangde800 题目链接: 点击打开链接 题意 有n个仓库,让m个人来看管.一个仓库只能由一个人来看管,一个人可以看管多个仓库. 每个人 ...

  9. openssh相关

    openssh相关    DSA RSA,非对称加密,产生公钥.私钥,前者存放在remote,后者存放在local,ssh-keygen产生公钥私钥时,提示输入私钥密码,防止私钥泄露被盗    ssh ...

  10. 定时任务框架-quartz

    依赖 <!-- 定时任务jar --> <dependency> <groupId>org.quartz-scheduler</groupId> < ...