这道题是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. lowbit

    树状数组(lowbit) Time Limit:1000ms   Memory Limit:128MB 题目描述 这天,LYK在学习树状数组. 当它遇到一个叫lowbit的函数时有点懵逼.lowbit ...

  2. 基于Java实现的插入排序算法

    简述 插入排序(Insertion-Sort)的算法描述是一种简单直观的排序算法.它的工作原理是通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入.插入排序在实现上,通常 ...

  3. table表格字母无法换行

    在项目中,用到的table比较多,本来布局挺好的,后来在td内写入英文字母,整个布局就乱了,会撑的很宽,不换行,后来才知道:一般字母的话会被浏览器默认是一个字符串或者说一个单词,所以不会自动换行. 于 ...

  4. WebService学习之旅(三)JAX-WS与Spring整合发布WebService

    Spring本身就提供了对JAX-WS的支持,有兴趣的读者可以研究下Spring的Spring-WS项目,项目地址: http://docs.spring.io/spring-ws/sites/1.5 ...

  5. 【NumPy学习指南】day5 改变数组的维度

    我们已经学习了怎样使用reshape函数,现在来学习一下怎样将数组展平. (1) ravel 我们可以用ravel函数完成展平的操作: In: b Out: array([[[ 0, 1, 2, 3] ...

  6. win7下如何解决协议适配器错误问题

    数据库为oracle 11g,在cmd中使用sqlplus命令出现了“协议适配器错误”. 原因分析:oracle相关服务没有启动. 解决办法如下: step1:进入服务页面. 方法一:cmd → se ...

  7. angulajs中引用chart.js做报表,修改线条样式

    目前还有个问题,在手机上看,当折线y轴值超过1000,会有点问题 1.下载chart js,可以用bower 命令下载 http://www.chartjs.org/docs/#line-chart- ...

  8. POJ 1655 Balancing Act (树的重心,常规)

    题意:求树的重心,若有多个重心,则输出编号较小者,及其子树中节点最多的数量. 思路: 树的重心:指的是一个点v,在删除点v后,其子树的节点数分别为:u1,u2....,设max(u)为其中的最大值,点 ...

  9. 通过StringBuilder的reverse()实现倒序

    import java.util.Scanner; public class ReverseString { public static void main(String[] args) { Scan ...

  10. 如何修改IOS的默认字体

    The first is workaround wich is iterating over all the labels in your UIView and change the labels f ...