/*
* @lc app=leetcode.cn id=141 lang=c
*
* [141] 环形链表
*
* https://leetcode-cn.com/problems/linked-list-cycle/description/
*
* algorithms
* Easy (35.53%)
* Total Accepted: 28.4K
* Total Submissions: 79.4K
* Testcase Example: '[3,2,0,-4]\n1'
*
* 给定一个链表,判断链表中是否有环。
*
* 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。
*
*
*
* 示例 1:
*
* 输入:head = [3,2,0,-4], pos = 1
* 输出:true
* 解释:链表中有一个环,其尾部连接到第二个节点。
*
*
*
*
* 示例 2:
*
* 输入:head = [1,2], pos = 0
* 输出:true
* 解释:链表中有一个环,其尾部连接到第一个节点。
*
*
*
*
* 示例 3:
*
* 输入:head = [1], pos = -1
* 输出:false
* 解释:链表中没有环。
*
*
*
*
*
*
* 进阶:
*
* 你能用 O(1)(即,常量)内存解决此问题吗?
*
*/
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
bool hasCycle(struct ListNode *head) {
struct ListNode *p = head,*q = head;
if(p == NULL)
return false;
else
{
while()
{
if(p->next == NULL||p->next->next == NULL)
return false;
q = q->next;
p = p->next->next;
if(p == q)
return true;
}
}
}

题目很好理解。思路也很明确,这里判断结点的下一个和节点的下一个的下一个是否能循环连接起来,就可以判断是否是环形链表。

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

python:

class Solution(object):
def hasCycle(self, head):
"""
:type head: ListNode
:rtype: bool
"""
fast = slow = head
while fast and fast.next:
fast = fast.next.next
slow = slow.next
if slow == fast:
return True
return False

Leecode刷题之旅-C语言/python-141环形链表的更多相关文章

  1. Leecode刷题之旅-C语言/python-203移除链表元素

    /* * @lc app=leetcode.cn id=203 lang=c * * [203] 移除链表元素 * * https://leetcode-cn.com/problems/remove- ...

  2. Leecode刷题之旅-C语言/python-83删除排序链表中的重复元素

    /* * @lc app=leetcode.cn id=83 lang=c * * [83] 删除排序链表中的重复元素 * * https://leetcode-cn.com/problems/rem ...

  3. Leecode刷题之旅-C语言/python-1.两数之和

    开学后忙的焦头烂额(懒得很),正式开始刷leecode的题目了. 想了想c语言是最最基础的语言,虽然有很多其他语言很简单,有更多的函数可以用,但c语言能煅炼下自己的思考能力.python则是最流行的语 ...

  4. Leecode刷题之旅-C语言/python-387 字符串中的第一个唯一字符

    /* * @lc app=leetcode.cn id=387 lang=c * * [387] 字符串中的第一个唯一字符 * * https://leetcode-cn.com/problems/f ...

  5. Leecode刷题之旅-C语言/python-28.实现strstr()

    /* * @lc app=leetcode.cn id=28 lang=c * * [28] 实现strStr() * * https://leetcode-cn.com/problems/imple ...

  6. Leecode刷题之旅-C语言/python-7.整数反转

    /* * @lc app=leetcode.cn id=7 lang=c * * [7] 整数反转 * * https://leetcode-cn.com/problems/reverse-integ ...

  7. Leecode刷题之旅-C语言/python-434 字符串中的单词数

    /* * @lc app=leetcode.cn id=434 lang=c * * [434] 字符串中的单词数 * * https://leetcode-cn.com/problems/numbe ...

  8. Leecode刷题之旅-C语言/python-326 3的幂

    /* * @lc app=leetcode.cn id=326 lang=c * * [326] 3的幂 * * https://leetcode-cn.com/problems/power-of-t ...

  9. Leecode刷题之旅-C语言/python-263丑数

    /* * @lc app=leetcode.cn id=263 lang=c * * [263] 丑数 * * https://leetcode-cn.com/problems/ugly-number ...

随机推荐

  1. 【Leetcode】【Easy】Merge Two Sorted Lists .

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  2. PPTP vs. OpenVPN vs. L2TP/IPsec vs. SSTP

    Which is the Best VPN Protocol? PPTP vs. OpenVPN vs. L2TP/IPsec vs. SSTP Want to use a VPN? If you’r ...

  3. ESP32D0WDQ6 灯泡 黑客

    这个黑客表现得如何聪明 灯泡 可能泄漏您的Wi-Fi密码O网页链接破解者博客详文 Pwn the LIFX Mini white O网页链接ESP32D0WDQ6, a SoC from ESPRES ...

  4. CentOS 7 Nginx+PHP环境搭建!

    1.Nginx 安装 rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx. ...

  5. 为什么要使用base64编码,有哪些情景需求?

    Base64编码原理与应用 Java实现BASE64编解码 公钥证书也好,电子邮件数据也好,经常要用到Base64编码,那么为什么要作一下这样的编码呢? 我们知道在计算机中任何数据都是按ascii码存 ...

  6. Windows下设置Ubuntu引导项

    最近在进行一些实验环境的配置,最终通过双系统实现了多系统的管理,而不仅限于虚拟机的方式.以此方式成功安装了Windows8.1 Pro和Windows 10,原文在此. 在此基础上进一步安装了 Ubu ...

  7. phonegap 的指南针 api Compass

    一. Compass 介绍方法参数   1.Compass 也就是,常说的指南针,又叫罗盘 2.方法 compass.getCurrentHeading compass.watchHeading co ...

  8. 牛客网多校训练第一场 J - Different Integers(树状数组 + 问题转换)

    链接: https://www.nowcoder.com/acm/contest/139/J 题意: 给出n个整数的序列a(1≤ai≤n)和q个询问(1≤n,q≤1e5),每个询问包含两个整数L和R( ...

  9. BZOJ1923:[SDOI2010]外星千足虫(高斯消元)

    Description Input 第一行是两个正整数 N, M. 接下来 M行,按顺序给出 Charles 这M次使用“点足机”的统计结果.每行 包含一个“01”串和一个数字,用一个空格隔开.“01 ...

  10. js 模拟百度关键字搜索与跳转

    测试效果: css样式: ul{ display:none; } html代码: <input type="text" id="text" /> & ...