/**
* 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. SharePoint REST API - 使用REST接口对列表设置自定义权限

    博客地址:http://blog.csdn.net/FoxDave SharePoint网站.列表和列表项都属于SecurableObject类型.默认情况下,一个安全对象继承父级的权限.对一个对 ...

  2. dos脚本2

    一.简单批处理内部命令简介  1.Echo 命令  打开回显或关闭请求回显功能,或显示消息.如果没有任何参数,echo 命令将显示当 前回显设置.  语法  echo [{on off}] [mess ...

  3. Java中==和equals的比较

    1.== (1)基本数据类型:比较值  基本类型有8种: 整数型:byte 字节类型:1个字节(8位) short 短整型:2个字节(16位) int  整数类型:4个字节(32位)   Java默认 ...

  4. python学习之路08——元组、集合

    一. 元组 1.概念 列表:是一种有序的集合 元组和列表的区别: ​ a.列表:[] 元组:() ​ b.列表中的元素可以进行增加和删除操作,但是,元组中的元素不能进行修改 ​ c.相同点:都是一种容 ...

  5. Greenplum(4.3.73)集群安装手册

    1. 概述 本文档仅限于指导Greenplum 4.3.7.3(对应安装包greenplum-db-4.3.7.3-build-2-RHEL5-x86_64.bin)版本在CentOS6.5 系统进行 ...

  6. 深入学习Motan系列(五)—— 序列化与编码协议

    一.序列化 1.什么是序列化和反序列化? 序列化:将对象变成有序的字节流,里面保存了对象的状态和相关描述信息. 反序列化:将有序的字节流恢复成对象. 一句话来说,就是对象的保存与恢复. 为什么需要这个 ...

  7. AJAX 请求后使用 JS 打开新标签页被阻止的解决方法

    需求:发起一个 AJAX 请求,根据请求结果来打开一个新页面. 问题:AJAX 请求后,使用 window.open() 方法来打开新页面会被浏览器阻止. 解决方法:在 AJAX 请求之前,就使用 c ...

  8. CSS3 实现圆形、椭圆形、三角形等各种形状样式

    CSS3 圆形 #css3-circle{ width: 150px; height: 150px; border-radius: 50%; background-color: #232323;} C ...

  9. java应用:向用户注册的邮箱发送邮件

    实现功能 忘记密码,注册成功等向用户发送验证码信息或注册信息. 业务流程 忘记密码: 1.验证邮箱是否注册过: 2.向邮箱发送验证码: 3.验证验证码是否正确: 4.重新设置密码: 我这里着重介绍发送 ...

  10. 第二章 JavaScript文档(上)

    JavaScript 1.JavaScript简介 起源 在1995年时,由Netscape公司的Brendan Eich,在网景导航者浏览器上首次设计实现而成.Netscape在最初将其脚本语言命名 ...