/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution
{
public bool HasCycle(ListNode head)
{
//if (head == null)
//{
// return false;
//}
//else
//{
// var temp = head;
// while (head.next != null)
// {
// var cur = head.next;
// if (temp == cur)
// {
// return true;
// }
// else
// {
// head = head.next;
// }
// }
// return false;
//} if (head == null) return false;
ListNode walker = head;
ListNode runner = head;
while (runner.next != null && runner.next.next != null)
{
walker = walker.next;
runner = runner.next.next;
if (walker == runner) return true;
}
return false;
}
}

https://leetcode.com/problems/linked-list-cycle/#/description

补充一个python的版本:

 class Solution:
def hasCycle(self, head: ListNode) -> bool:
if head == None:
return False
slow,fast = head,head
while fast != None and fast.next != None:
slow = slow.next
fast = fast.next.next
if slow == fast:
return True
return False

leetcode141的更多相关文章

  1. 每天一道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 ...

  2. [LeetCode141]Linked List Cycle

    题目:Given a linked list, determine if it has a cycle in it. 判断一个链表是否有环 代码: /** * Definition for singl ...

  3. [Java]LeetCode141. 环形链表 | 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. LeetCode141.环形链表

    给定一个链表,判断链表中是否有环. 进阶:你能否不使用额外空间解决此题? /** * Definition for singly-linked list. * class ListNode { * i ...

  5. LeetCode141:Linked List Cycle

    题目: Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without usin ...

  6. LeetCode141 Linked List Cycle. LeetCode142 Linked List Cycle II

    链表相关题 141. Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can y ...

  7. LeetCode141 环形链表(Java—HashSet简单应用or双指针)

    题目: 判断给出的链表中是否存在环. 思路: 1. 遍历整个链表,将走过的节点的内存地址保存下来,如果再次走到同样的内存地址,说明链表中有环.时间复杂度为O(n). 2. 设置两个指针,fast指针每 ...

  8. LeetCode链表解题模板

    一.通用方法以及题目分类 0.遍历链表 方法代码如下,head可以为空: ListNode* p = head; while(p!=NULL) p = p->next; 可以在这个代码上进行修改 ...

  9. LeetCode通关:听说链表是门槛,这就抬脚跨门而入

    分门别类刷算法,坚持,进步! 刷题路线参考:https://github.com/youngyangyang04/leetcode-master       https://github.com/ch ...

随机推荐

  1. Java语法基础学习DayTen(集合续)

    一.集合 1.Set:存储的元素是无序的.不可重复的 (1)无序性:无序性不等于随机性,无序指的是元素在底层存储的位置是无序的. (2)不可重复性:当向Set中添加相同的元素时,后添加的元素不能添加进 ...

  2. scrapy中crawlspide中callback和follow函数的作用及使用方法

    Rule(LinkExtractor(allow=r'i/tems'),callback='parse_item',follow=True)  当前代码的含义就是将当前页面及按照allow=r'i/t ...

  3. Windows10 VS2017 C++ ini解析(使用simpleini头文件)

    simpleini项目地址: https://github.com/brofield/simpleini 下载,新建项目,并将SimpleIni.h文件通过包含目录的方式加载进来. 创建test.in ...

  4. ReactiveCocoa基础

    在讲ReactiveCocoa之前,我们来回忆一下Block在开发中的使用场景: 1.把block保存到对象中,等到恰当的时候才去调用 2.把block当做方法的参数使用,外界不调用,都是方法内部去调 ...

  5. python实现简单的定时任务

    1.首先安装 schedule 模块 命令行安装 pip install schedule pyCharm编辑器安装 File->setting->project:youProject-& ...

  6. CentOS6.5上安装MySQL

    1.查看操作系统的相关信息 2.查看系统上所有MySQL的rpm包并删除 [root@master ~]# rpm -qa | grep -i mysql [root@masterc ~]# yum ...

  7. 源代码安装-非ROOT用户安装软件的方法

    0.    前言 如果你没有sudo权限,则很多程序是无法使用别人编译好的文件安装的. 还有时候,没有对应你的主机配置的安装包,这时候需要我们自己下载最原始的源代码,然后进行编译安装. 这样安装的程序 ...

  8. $inject

    function breadcrumb($parse, store) { } // 为了压缩 breadcrumb.$inject = ['$parse', 'breadcrumbStore']

  9. PyCharm:ModuleNotFoundError: No module named 'selenium'

    Mac安装PyCharm后,将已有工程导入,之前使用Mac终端执行脚本时正常,现在报错ModuleNotFoundError: No module named 'selenium',解决方法是在PyC ...

  10. Centos6.5搭建Elasticsearch

    ElasticSearch是基于Lucene的搜索服务.支持分布式多用户能力的全文搜索引擎,提供RESTful web接口.Elasticsearch是用Java开发的,Apache旗下开源项目,支持 ...