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

Follow up:
Can you solve it without using extra space?

判断链表中是否有环,不能用额外的空间,可以使用快慢指针,慢指针一次走一步,快指针一次走两步,若是有环则快慢指针会相遇,若是fast->next==NULL则没有环。

值得注意的是:在链表的题中,快慢指针的使用频率还是很高,值得注意。

 /**
* 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 *pFast=head;
ListNode *pSlow=head; while(pFast&&pFast->next)
{
pSlow=pSlow->next;
pFast=pFast->next->next;
if(pFast==pSlow)
return true;
}
return false;
}
};

[Leetcode] Linked list cycle 判断链表是否有环的更多相关文章

  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. LeetCode 141. Linked List Cycle(判断链表是否有环)

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

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

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

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

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

    题目意思:链表有环,返回true,否则返回false 思路:两个指针,一快一慢,能相遇则有环,为空了没环 ps:很多链表的题目:都可以采用这种思路 /** * Definition for singl ...

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

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

随机推荐

  1. Learning Experience of Big Data: Learn to install CentOs 6.5 on my laptop

    I have learnt some experience about Big Data during my summer vocation,I was told that The first thi ...

  2. Python学习 :格式化输出

    方式一:使用占位符 % 常用占位符:% s   (s = string 字符串)     % d   (d = digit 整数(十进制))   %  f   ( f = float  浮点数) na ...

  3. WPF中InkCanvas(墨水面板)用法

    原文:WPF中InkCanvas(墨水面板)用法   WPF中InkCanvas(墨水面板)用法                                                    ...

  4. Java线程和多线程(十)——TimerTask

    Java中的java.util.Timer是一个工具类,可以用于调度一个线程在将来的某一个时刻执行特定的任务.Java Timer类可以将一个任务定时执行一次,或者是以后以每隔一定的时间间隔来触发一次 ...

  5. Memcached Hash算法

    本文来自网易云社区 作者:吕宗胜 Hash算法 1. Memcached Hash介绍 我们在前面的文章中已经介绍过了Memcached的内存管理方式,LRU的策略.由于Memcached的数据存储方 ...

  6. mysql连接jdbc查询代码

    package com.answer.test; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.S ...

  7. Python-学习-小例子练习

    网上了点小例子,练习一下下,都是特别简单的.而且这些代码也都是找的网上的代码,目的是在于练习一下Python和熟悉下Python的编码风格等等 学习一门语言,最快的方法就是把它用在世界的开发中,这样才 ...

  8. 单链表无head各种操作及操作实验

    #encoding=utf-8 class ListNode: def __init__(self,x): self.val=x; self.next=None;   #链表逆序 def revers ...

  9. JavaScript调试中Console命令

    JS调试中,用console.log 感觉比 alert 好用,不用弹出窗口,还要关闭.除了console.log()其他命令没怎么用过,先在这里记一下,用到时在看看 一.显示信息的命令 consol ...

  10. Mysql性能优化一:SQL语句性能优化

    这里总结了52条对sql的查询优化,下面详细来看看,希望能帮助到你 1, 对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2,应尽量避免在 w ...