leetcode141
- /**
- * 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的更多相关文章
- 每天一道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 ...
- [LeetCode141]Linked List Cycle
题目:Given a linked list, determine if it has a cycle in it. 判断一个链表是否有环 代码: /** * Definition for singl ...
- [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 ...
- LeetCode141.环形链表
给定一个链表,判断链表中是否有环. 进阶:你能否不使用额外空间解决此题? /** * Definition for singly-linked list. * class ListNode { * i ...
- LeetCode141:Linked List Cycle
题目: Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without usin ...
- 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 ...
- LeetCode141 环形链表(Java—HashSet简单应用or双指针)
题目: 判断给出的链表中是否存在环. 思路: 1. 遍历整个链表,将走过的节点的内存地址保存下来,如果再次走到同样的内存地址,说明链表中有环.时间复杂度为O(n). 2. 设置两个指针,fast指针每 ...
- LeetCode链表解题模板
一.通用方法以及题目分类 0.遍历链表 方法代码如下,head可以为空: ListNode* p = head; while(p!=NULL) p = p->next; 可以在这个代码上进行修改 ...
- LeetCode通关:听说链表是门槛,这就抬脚跨门而入
分门别类刷算法,坚持,进步! 刷题路线参考:https://github.com/youngyangyang04/leetcode-master https://github.com/ch ...
随机推荐
- nginx——限制上传文件的大小
client_max_body_size 用于设置最大的允许客户端请求主体的大小,在请求首部中有 "Content-Length" ,如果超过了此配置项,客户端会收到 413 错误 ...
- uboot kernel 博客
https://blog.csdn.net/zqixiao_09/ https://home.cnblogs.com/u/lifexy/ https://blog.csdn.net/chenliang ...
- Linux 文件类型笔记
在UNIX中一切都是文件https://ph7spot.com/musings/in-unix-everything-is-a-file在UNIX中,一切都是字节流 ==== linux系统的文件类型 ...
- 《Linux内核原理与分析》第三周作业
实验:基于kernel的简单的时间片轮转多道程序内核 1.实验要求 完成一个简单的时间片轮转多道程序内核代码 2.实验过程 进入实验楼的linux环境,打开shell,输入以下代码: cd Linux ...
- zombodb 几点说明
内容来自官方文档,截取部分 默认es 索引的副本为0 这个参考可以通过修改索引,或者在创建的时候通过with 参数指定,或者通过pg 的配置文件中指定 索引更多的列以为这使用了更多的es 能力 索引的 ...
- dubbo 基础入门
一.什么是dubbo? Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案,说白了就是个远程服务调用的分布式框架. dubbo产生的背景 ① 单一 ...
- Docker Compose(八)
Docker Compose 是Docker官方编排(Orchstration)项目之一,负责快速在集群中部署分布式应用. Dockerfile可以让用户管理一个单独的应用容器:而Compose则 ...
- Vue的理解:Vue.js新手入门指南----转
最近在逛各大网站,论坛,以及像SegmentFault等编程问答社区,发现Vue.js异常火爆,重复性的提问和内容也很多,楼主自己也趁着这个大前端的热潮,着手学习了一段时间的Vue.js,目前用它正在 ...
- Spring Cloud(Dalston.SR5)--Zuul 网关-Hystrix 回退
当我们对网关进行配置让其调用集群的服务时,将会执行 Ribbon 路由过滤器,该过滤器在进行转发时会封装为一个 Hystrix 命令予以执行,Hystrix 命令具有容错的功能,如果"源服务 ...
- javaScript 中的私有,共有,特权属性和方法
function constructor () { var private_v; // 私有属性 var private_f = function () { // 私有方法 // code }; th ...