/*
* @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. Python初学者第十一天 文件处理_batch

    11day 文件的操作分为读.写.修改 1.读: f = open(file='D:\新建文本文档.txt',mode='r',encoding='gbk') data = f.read() prin ...

  2. js函数 标签: javascript 2016-08-12 16:48 56人阅读 评论(0) 收藏

    函数实际上是对象,函数名实际上也是一个指向函数对象的指针. 使用不带圆括号的函数名是访问函数指针,而非调用函数. 函数声明和函数表达式: alert(test(2,3)); function test ...

  3. 获取所有权windows目录所有权

    Takeown /r /f 盘符:\目录\目录 例如: Takeown /r /f C:\Windows\CSC

  4. 查看Oracle表中的指定记录在数据文件中的位置

    查看Oracle表中的指定记录位置select rowid,user_id from sshr.xx_user where user_id=3010586 select rowid,       db ...

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

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

  6. luogu P1522 Cow Tours

    嘟嘟嘟 题面挺绕的,“翻译”一下: 1.牧区是一个点,牧场是所有直接相连的点构成的联通块. 2.两个牧区之间的距离是这两个距离之间的最短路,只有直接相连的两个牧区之间的距离是欧几里得距离. 3.牧场的 ...

  7. 本地测试时修改localhost为自己网站的域名的方法(转载)

    做网站的,在本地测试时,所用的地址基本上都是localhost 或者直接用IP地址:127.0.0.1 如果仅仅是用来测试网站内部的程序代码之类的当然没问题,但是如果我们还要测试网站上添加的广告或者统 ...

  8. 获取Windows安装日期

  9. 关于Ftp服务器

    步骤/方法 首先在本地机器上创建一个用户!这些用户是用来登录到FTP的!我的电脑右键->管理->本地用户和组->用户->“右键”新建用户->输入用户名和密码再点创建就行了 ...

  10. c#用链表来存储并读取写好的配置文件

    别用arraylist,效果没有list好(因为要装箱拆箱,所以会影响性能) 使用list,那我们就来先声明一个List 1) 声明 List<元素类型> myList = new Lis ...