Leetcode 141. Linked List CycleJAVA语言
1
2
3
|
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
|
题意:判断链表有没有环
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public boolean hasCycle(ListNode head) {
///定义两个快慢指针。有环的话快指针一定会和慢指针相遇。否则快指针就提前到尾巴了
应该是我那会的考研题ca
if (head== null || head.next== null || head.next.next== null ) return false ;
ListNode fast=head.next.next;
ListNode slow=head.next;
while (fast!=slow){
if (fast.next!= null && fast.next.next!= null ){
fast=fast.next.next;
slow=slow.next;
} else {
//fast提前走到了尾
return false ;
}
}
return true ;
}
}
|
PS:快慢指针。注意判断边界条件
本文转自 努力的C 51CTO博客,原文链接:http://blog.51cto.com/fulin0532/1905474
Leetcode 141. Linked List CycleJAVA语言相关推荐
- LeetCode 141. Linked List Cycle--面试编程题--C++,Python解法
题目地址:Linked List Cycle - LeetCode Given a linked list, determine if it has a cycle in it. To represe ...
- 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 ex ...
- 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 ex ...
- [LeetCode] 141. Linked List Cycle 单链表判圆算法
TWO POINTER 快指针速度2 , 慢指针速度1 相对速度1,有环必然相遇 public class Solution {public boolean hasCycle(ListNode hea ...
- LeetCode 141 Linked List Cycle
用快慢指针来判定是否有环. 这里while loop里的条件,用的是fast.next != null && fast.next.next != null,保证如果没有环,slow一定 ...
- Leetcode 142 Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- 141. Linked List Cycle
欢迎fork and star:Nowcoder-Repository-github 141. Linked List Cycle 题目 Given a linked list, determine ...
- Leetcode 142. Linked List Cycle II
地址:Leetcode 142. linked list Cycle II 问题描述:检测链表是否存在环,是的话返回环入口,否则返回None. 这道题有两个思路,一个是经典的快慢指针的思路,另外一个是 ...
- 【To Do】LeetCode 142. Linked List Cycle II
LeetCode 142. Linked List Cycle II Solution1:我的答案 这道题多次遇到,牢记此解法 这道题要深思一下,快指针和慢指针的速度对比不同,会产生什么不同的结果? ...
最新文章
- 学python可以做什么产品-学完Python可以做什么?主要用途有哪些?
- 后台技术-JavaWeb项目初识
- 华为电视测试软件,华为智慧屏电视安装第三方软件,简单实测可行攻略
- [leetcode]106.从中序与后序遍历序列构造二叉树
- Android 从AndroidManifest获取meta-data
- pytorch已经安装成功了为什么不能使用import_使用auto keras的过程
- 电子表格转换成数据库_创建数据库,将电子表格转换为关系数据库,第1部分...
- Github | Google开源高性能机器学习研究工具Jax
- Dameware Mini Remote Control 连接记录恢复
- 拓端tecdat|r语言聚类分析:k-means和层次聚类
- 推荐一个js代码混淆工具的网址
- LM2596电源降压调整器(150KHz,3A)020
- 中易浏览器(enjoyie) v3.0.1.15 完整绿色版 免费
- 语音合成(TTS)论文优选:Learning to Speak Fluently in a Foreign Language: Multilingual Speech Synthesis and Cr
- python提取pdf内容_别再问如何用Python提取PDF内容了!
- 记一次批量定时抓取微信公众号文章的实现
- 思科路由器各种show命令的输出详解
- 解析微分电路和积分电路的区别
- 简单的VUE购物车应用
- 水星MW150US完美驱动10.6.X
热门文章
- java线程占用CPU_在windows下揪出java程序占用cpu很高的线程并完美解决
- java integer 不变模式_Java代码的变与不变
- gitlab mysql 表_gitlab-mysql_高可用
- 学习笔记Hadoop(十一)—— Hadoop基础操作(3)—— MapReduce常用Shell操作、MapReduce任务管理
- c# string 转 datetime_C#判断语句详解(if、if-else、switch-case、三目/元运算符)
- java语言 文件上传,java中实现文件上传的方法
- 垃圾热解气化工艺的电气、仪表及控制系统设计
- np.append()
- 百兆以太网传输距离_新品推荐 16+2G口百兆/千兆监控接入型非标PoE交换机!
- sdutoj-3837-素数链表