这道题是LeetCode里的第141道题。

题目要求:

给定一个链表,判断链表中是否有环。

进阶:

你能否不使用额外空间解决此题?

简单题,但是还是得学一下这道题的做法,这道题是用双指针一个fast,一个slow。fast每一步前进两个节点,slow前进一个节点。判断fast和slow是否相等或者为空就行了。

贴个代码:

/**
* 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 *fast,*slow;
if(head==NULL||head->next==NULL)
return false;
fast=head->next;
slow=head;
while(fast!=slow){
if(fast->next!=NULL&&fast->next->next!=NULL){
fast=fast->next->next;
slow=slow->next;
}
else
return false;
}
return true;
}
};

贴个结果:

个人总结:

我最初的想法就是一个动指针一个定指针来循环判断是否为环,这个方法在时间上要比我之前的快。

【LeetCode】Linked List Cycle(环形链表)的更多相关文章

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

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

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

  4. [LeetCode] Linked List Cycle II 链表环起始位置

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

  5. 141 Linked List Cycle 环形链表

    给定一个链表,判断链表中否有环.补充:你是否可以不用额外空间解决此题?详见:https://leetcode.com/problems/linked-list-cycle/description/ J ...

  6. LeetCode Linked List Cycle 单链表环

    题意:给一个单链表,判断其是否出现环! 思路:搞两个指针,每次,一个走两步,另一个走一步.若有环,他们会相遇,若无环,走两步的指针必定会先遇到NULL. /** * Definition for si ...

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

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

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

随机推荐

  1. DDX和DDV——控件与变量之间值的传递

    DoDataExchange由框架调用,作用是交互并且验证对话框数据,主要由(DDX) 和 (DDV)宏实现. 永远不要直接调用这个函数,而是通过UpdateData(TRUE/FALSE)实现控件与 ...

  2. 在WIN7、WIN8中,将快捷方式锁定到任务栏,C#

    其实很简单,使用 API 函数 ShellExecute,就可以解决这个问题. 首先添加引用 using System.Runtime.InteropServices; 代码如下: using Sys ...

  3. 流行的9个Java框架介绍: 优点、缺点等等

    流行的9个Java框架介绍: 优点.缺点等等 在 2018年,Java仍然是世界上最流行的编程语言.它拥有一个巨大的生态系统,在全世界有超过900万Java开发人员.虽然Java不是最直接的语言,但是 ...

  4. vim编辑器高级应用

    1. vim主要模式介绍 命令模式.命令行模式.编辑模式 字符操作:i 当前插入, I行首插入, a当前字符之后插入,A行首插入, ESC退出当前模式 2. vim命令模式 3. vim插入模式 4. ...

  5. 由于js词法性质和全局变量被更改,循环绑定的click事件执行时变量和定义时 不一致的bug,各种解决方案。

    由于js词法性质和全局变量被更改,循环绑定的click事件执行时变量和定义时 不一致的bug,各种解决方案. 动态在页面上添加了5个按钮,实现的功能应该是点击对应按钮在控制台输出相应的索引.但因为应该 ...

  6. Web开发入门不得不看章

    引 如今,各种互联网的Web应用程序层出不穷,那么如何快速入门,成长为一个优秀的Web开发工作者呢? 这个问题不容易回答,几乎所有的培训机构都不能清晰地解答. 所以对于Web开发刚刚入门的菜鸟们,我觉 ...

  7. “Debug Assertion” Runtime Error on VS2008 VS2010 winhand.cpp

    I'm writing a C++ MFC program on VS2008 and I'm getting this "Debug Assertion Error" when ...

  8. uvm_agent——007(特工)

    詹姆斯·邦德作为007的代言人,很好地诠释了agent的含义.但是在计算机系统中agent(代理)指能自主活动的软件或者硬件实体.在UVC中agent作为容器,实例化VIP的所有模块包括driver, ...

  9. github入门之分支操作--5

    1.显示分一览表 2.创建.切换分支 2.1.切换到feature-A分支并进行提交 2.1.1.执行下面的命令,创建名为feature-A的分支 实际上,执行以命令也能收到同样的效果,但是我习惯使用 ...

  10. svn merge当主干修改后合并分支

    例如版本r1的主干创建分支r2,在r2上修改后得到r3,r1之后也修改得到r4,现在合并分支到主干上: 如果r3的修改和r4有冲突会提示出现冲突,因此不用担心主干合并后会被分支操作覆盖,因为这并不是简 ...