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

To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexed) in the linked list where tail connects to. If pos is -1, then there is no cycle in the linked list.

Example 1:

Input: head = [3,2,0,-4], pos = 1
Output: true
Explanation: There is a cycle in the linked list, where tail connects to the second node.

Example 2:

Input: head = [1,2], pos = 0
Output: true
Explanation: There is a cycle in the linked list, where tail connects to the first node.

Example 3:

Input: head = [1], pos = -1
Output: false
Explanation: There is no cycle in the linked list.

Follow up:

Can you solve it using O(1) (i.e. constant) memory?

------------------------------------------------------------------------------------------------------------------------------------------------------

用中文来说,这个题就是判断一个链表是否存在环。可以用双指针来解决。即可以建立一个慢指针和快指针,最后两个指针相遇,就可以判断相等。

C++代码:

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
bool hasCycle(struct ListNode *head) {
struct ListNode *slow,*fast;
slow = head;
fast = head;
while(fast && fast->next){ //如果fast=NULL或fast->next=NULL就说明了链表一定没有环,只是一个单链表而已。
slow = slow->next;
fast = fast->next->next;
if(slow == fast)
return true;
}
return false;
}

(链表 双指针) leetcode 141. Linked List Cycle的更多相关文章

  1. (链表 双指针) leetcode 142. Linked List Cycle II

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

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

  3. 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现

    引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ...

  4. leetcode 141. Linked List Cycle 、 142. Linked List Cycle II

    判断链表有环,环的入口结点,环的长度 1.判断有环: 快慢指针,一个移动一次,一个移动两次 2.环的入口结点: 相遇的结点不一定是入口节点,所以y表示入口节点到相遇节点的距离 n是环的个数 w + n ...

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

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

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

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

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

随机推荐

  1. Lodop打印控件设置表格次页偏移

    Lodop打印控件有很好的自动分页功能,超文本table表格一页装不下,自动分到第二页,第三页……通常表格之前还会有一些内容,比如标题,制表人名称日期什么的杂七杂八的东西,这种东西会占用一定的空间,这 ...

  2. OfficeToHtmlHelper

    public class Office2HtmlHelper { /// <summary> /// Word转成Html /// </summary> /// <par ...

  3. PC平台的SIMD支持检测

    如果我们希望在用SIMD来提升程序处理的性能,首先需要做的就是检测程序所运行的平台是否支持相应的SIMD扩展.平台对SIMD扩展分为两部分的支持: CPU对SIMD扩展的支持.SIMD扩展是随着CPU ...

  4. JarvisOJ Basic easyRSA

    还记得veryeasy RSA吗?是不是不难?那继续来看看这题吧,这题也不难. 已知一段RSA加密的信息为:0xdc2eeeb2782c且已知加密所用的公钥: (N=322831561921859 e ...

  5. 解决Error:com.intellij.util.indexing.StorageException

    删除 C:\Users\Nihaorz\.IntelliJIdea2017.1\system\compile-server 目录下的所有内容即可

  6. centos6.8下普通用户下quartus编程识别不到用户开发板

    前言 centos6.8下su用户下可以识别到用户的板子,能正常下板,而普通用户下无法探测到用户板子. 需要配置一下usb-blaster,不然的话,虽然quartus软件能识别,但是无法探测到FPG ...

  7. project 2013 删除资源

    1.分析 在资源名称这边一旦输入过资源名称,下次点击下拉框就会出现历史记录,如何删除 2.步骤 资源-->分配资源-->点击资源名称,按F2,按DEL键

  8. nginx 重定向 说明

    一.nginx 两个操作系统的安装见以前的随笔(已安装请跳过) linux上搭建nginx windows上搭建nginx 二.Nginx重定向——直接到项目,而非nginx欢迎页 默认ngin修改n ...

  9. python学习day5 数据类型Ⅲ(字典)

    day5 字典 回顾&补充 面试题 #数据类型判断a = 1 #intb = (1) #intc = ('1') #strd = (1,) #tuple int py2/py3 除法 强制转换 ...

  10. nodejs的某些api~(六)HTTPS

    node的HTTPS模块接口与HTTP其实差不多,就是多了一个认证证书,私钥的配置等等,API都相似的. 在客户端服务器通信的方法中,只有HTTPS是最安全的,它的原理是客户端和服务器发送自己的公钥, ...