1. /**
  2. * Definition for singly-linked list.
  3. * public class ListNode {
  4. * public int val;
  5. * public ListNode next;
  6. * public ListNode(int x) {
  7. * val = x;
  8. * next = null;
  9. * }
  10. * }
  11. */
  12. public class Solution
  13. {
  14. public bool HasCycle(ListNode head)
  15. {
  16. //if (head == null)
  17. //{
  18. // return false;
  19. //}
  20. //else
  21. //{
  22. // var temp = head;
  23. // while (head.next != null)
  24. // {
  25. // var cur = head.next;
  26. // if (temp == cur)
  27. // {
  28. // return true;
  29. // }
  30. // else
  31. // {
  32. // head = head.next;
  33. // }
  34. // }
  35. // return false;
  36. //}
  37.  
  38. if (head == null) return false;
  39. ListNode walker = head;
  40. ListNode runner = head;
  41. while (runner.next != null && runner.next.next != null)
  42. {
  43. walker = walker.next;
  44. runner = runner.next.next;
  45. if (walker == runner) return true;
  46. }
  47. return false;
  48. }
  49. }

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

补充一个python的版本:

  1. class Solution:
  2. def hasCycle(self, head: ListNode) -> bool:
  3. if head == None:
  4. return False
  5. slow,fast = head,head
  6. while fast != None and fast.next != None:
  7. slow = slow.next
  8. fast = fast.next.next
  9. if slow == fast:
  10. return True
  11. 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. nginx——限制上传文件的大小

    client_max_body_size 用于设置最大的允许客户端请求主体的大小,在请求首部中有 "Content-Length" ,如果超过了此配置项,客户端会收到 413 错误 ...

  2. uboot kernel 博客

    https://blog.csdn.net/zqixiao_09/ https://home.cnblogs.com/u/lifexy/ https://blog.csdn.net/chenliang ...

  3. Linux 文件类型笔记

    在UNIX中一切都是文件https://ph7spot.com/musings/in-unix-everything-is-a-file在UNIX中,一切都是字节流 ==== linux系统的文件类型 ...

  4. 《Linux内核原理与分析》第三周作业

    实验:基于kernel的简单的时间片轮转多道程序内核 1.实验要求 完成一个简单的时间片轮转多道程序内核代码 2.实验过程 进入实验楼的linux环境,打开shell,输入以下代码: cd Linux ...

  5. zombodb 几点说明

    内容来自官方文档,截取部分 默认es 索引的副本为0 这个参考可以通过修改索引,或者在创建的时候通过with 参数指定,或者通过pg 的配置文件中指定 索引更多的列以为这使用了更多的es 能力 索引的 ...

  6. dubbo 基础入门

    一.什么是dubbo? Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案,说白了就是个远程服务调用的分布式框架. dubbo产生的背景 ① 单一 ...

  7. Docker Compose(八)

    Docker Compose 是Docker官方编排(Orchstration)项目之一,负责快速在集群中部署分布式应用.   Dockerfile可以让用户管理一个单独的应用容器:而Compose则 ...

  8. Vue的理解:Vue.js新手入门指南----转

    最近在逛各大网站,论坛,以及像SegmentFault等编程问答社区,发现Vue.js异常火爆,重复性的提问和内容也很多,楼主自己也趁着这个大前端的热潮,着手学习了一段时间的Vue.js,目前用它正在 ...

  9. Spring Cloud(Dalston.SR5)--Zuul 网关-Hystrix 回退

    当我们对网关进行配置让其调用集群的服务时,将会执行 Ribbon 路由过滤器,该过滤器在进行转发时会封装为一个 Hystrix 命令予以执行,Hystrix 命令具有容错的功能,如果"源服务 ...

  10. javaScript 中的私有,共有,特权属性和方法

    function constructor () { var private_v; // 私有属性 var private_f = function () { // 私有方法 // code }; th ...