[LeetCode]141. Linked List Cycle判断循环链表
快慢指针用来判断循环链表 记住
快慢指针有四种常用的应用场景:
1.找到有序链表的中点,快指针到头的时候,慢指针就是中点。
2.判断是不是循环链表,快慢指针相遇就是
3.找到循环链表的起点,以链表头结点开始的结点和以快慢指针相遇结点为开始的结点,这两个结点相遇的地方就是开始
4.判断两个链表是不是相交。将其中一个链表收尾相接,然后判断另外一个链表是不是循环链表。
有个博客写的很好:
http://blog.csdn.net/willduan1/article/details/50938210
看见循环链表就向快慢指针,就向看见BST就想中序遍历一样
public boolean hasCycle(ListNode head) {
/*
看的答案,快慢指针,如果一个链表是循环链表,那么快慢指针一定会相遇
因为快指针肯定会套慢指针一圈,就跟跑步套圈一样
*/
if (head==null) return false;
ListNode slow = head;
ListNode fast = head;
while (fast!=null&&fast.next!=null)
{
slow = slow.next;
fast = fast.next.next;
if (fast==slow) return true;
}
return false;
}
第二题是找到循环链表的开始
public ListNode detectCycle(ListNode head){
/*
快慢指针的一个应用,找到链表的循环开始的节点
方法是:一个节点的从链表开头开始,一个节点从快慢节点的相遇点开始,一起走,每次一步,相遇的地方是就是循环的开始
*/
if (head==null||head.next==null) return null;
//快慢指针找到相遇的地方
ListNode slow = head;
ListNode fast = head;
while (fast!=null&&fast.next!=null)
{
slow = slow.next;
fast = fast.next.next;
if (fast==slow) break;
}
//如果fast或者fast的next是null,说明没有环
if (fast==null||fast.next==null) return null;
slow = head;
while (fast!=slow)
{
fast = fast.next;
slow = slow.next;
}
return slow;
}
[LeetCode]141. Linked List Cycle判断循环链表的更多相关文章
- LeetCode 141. Linked List Cycle 判断链表是否有环 C++/Java
Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked lis ...
- [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 ...
- LeetCode 141. Linked List Cycle(判断链表是否有环)
题意:判断链表是否有环. 分析:快慢指针. /** * Definition for singly-linked list. * struct ListNode { * int val; * List ...
- 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现
引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ...
- leetcode 141. Linked List Cycle 、 142. Linked List Cycle II
判断链表有环,环的入口结点,环的长度 1.判断有环: 快慢指针,一个移动一次,一个移动两次 2.环的入口结点: 相遇的结点不一定是入口节点,所以y表示入口节点到相遇节点的距离 n是环的个数 w + n ...
- [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 ...
- 141. Linked List Cycle(判断链表是否有环)
141. Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you sol ...
- [LeetCode] 141. Linked List Cycle 单链表中的环
Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked lis ...
- 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 ...
随机推荐
- TensorFlow安装方法:附带坑解决办法
>>添加Anaconda 仓库的镜像 Anaconda 安装包可以到 https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/ 下载. ...
- 【2020.11.28提高组模拟】T1染色(color)
[2020.11.28提高组模拟]T1染色(color) 题目 题目描述 给定 \(n\),你现在需要给整数 \(1\) 到 \(n\) 进行染色,使得对于所有的 \(1\leq i<j\leq ...
- Django+Nginx+uWSGI生产环境部署
生产环境中的数据流 参考文档: wsgi详解:https://blog.csdn.net/li_101357/article/details/52748323 wsgi协议介绍(萌新版):https: ...
- 第7.13节 案例详解:Python类变量
第7.13节 案例详解:Python类变量 上节介绍了类变量的定义和使用方法,并举例进行了说明.本节将通过一个更完整的例子来说明. 一. 定义函数dirp def dirp(iter): ret ...
- 第14.10节 Python中使用BeautifulSoup解析http报文:html标签相关属性的访问
一. 引言 在<第14.8节 Python中使用BeautifulSoup加载HTML报文>中介绍使用BeautifulSoup的安装.导入和创建对象的过程,本节介绍导入后利用Beauti ...
- kaggle——Bag of Words Meets Bags of Popcorn(IMDB电影评论情感分类实践)
kaggle链接:https://www.kaggle.com/c/word2vec-nlp-tutorial/overview 简介:给出 50,000 IMDB movie reviews,进行0 ...
- ajax的五种状态
ajax的五种状态(readyState ) 0 - (未初始化)还没有调用send()方法 1 - (载入)已调用send()方法,正在发送请求 2 - (载入完成)send()方法执行完成,已经接 ...
- ORCHARD WOODEN GATE
狗: 代码小盒子 爆零秘籍 备忘录 任务计划 核心算法: 搜索/枚举/贪心 dp 分治 数据结构: 并查集 ST表 堆 线段树 树状数组 分块 树套树 平衡树 LCT 莫队 字符串: 哈希 Trie ...
- Canal监听mysql
安装mysql5.7,并开启binlog 安装mysql 开启binlog find / -name my.cnf 找到这个文件 添加几行 [mysqld] log-bin=mysql-bin # 开 ...
- I/O-外部设备
目录 输入设备 输出设备 显示器 阴极射线管(CRT)显示器 字符显示器 图形显示器 图像显示器 打印机 小结 外存储器 磁盘存储器 磁盘设备的组成 存储区域 硬盘存储器 磁盘的性能指标 磁盘地址 硬 ...