[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 ...
随机推荐
- java面试复习重点:类的管理及常用工具,教你抓住面试的重点!
java复习: 类的管理及常用工具类 包 写在程序文件的第一行 一个Java 源文件中只能声明一个包, 且声明语句只能作为源文件的第一条指令 导入类能导入非public类,但是不能用因为在其他包缺省的 ...
- Guava中EventBus分析
EventBus 1. 什么是EventBus 总线(Bus)一般指计算机各种功能部件之间传送信息的公共通信干线,而EventBus则是事件源(publisher)向订阅方(subscriber)发送 ...
- 第一次UML作业
这个作业属于哪个课程 https://edu.cnblogs.com/campus/fzzcxy/2018SE2/ 这个作业要求在哪里 https://edu.cnblogs.com/campus/f ...
- Fiddler 4 (利用Fiddler模拟恶劣网络环境)
1.模拟弱网环境 打开Fiddler,Rules->Performance->勾选 Simulate Modem Speeds,勾选之后访问网站会发现网络慢了很多 解决办法去掉勾选的地方网 ...
- Sql注入--数字型手工测试
Sql注入--数字型手工测试 漏洞原因:是在数据交互中,前端的数据传入到后台处理时,没有做严格的判断,导致其传入的"数据"拼接到SQL语句中后,被当作SQL语句的一部分执行. 从而 ...
- Python正则表达式re模块学习遇到的问题
Python正则表达式处理的组是什么? Python正则表达式处理中的匹配对象是什么? Python匹配对象的groups.groupdict和group之间的关系 Python正则表达式re.mat ...
- 从go-libp2p开始
这里是从一系列关于libp2p的go实现教程开始,go-libp2p 我们会讲述go的安装,go模块的设置,启动libp2p节点,并在它们之间发送消息. 安装go go-libp2p推荐使用包含 mo ...
- 【题解】P1852 跳跳棋
link 题意 跳跳棋是在一条数轴上进行的.棋子只能摆在整点上.每个点不能摆超过一个棋子.棋盘上有3颗棋子,分别在 \(a,b,c\) 这三个位置.我们要通过最少的跳动把他们的位置移动成 \(x,y, ...
- Java集合源码分析(五)——HashMap
简介 HashMap 是一个散列表,存储的内容是键值对映射. HashMap 继承于AbstractMap,实现了Map.Cloneable.java.io.Serializable接口. HashM ...
- Vue组件化开发(原有项目的改造)
将组件定义成一个js和css,然后供其他页面进行调用 demo.html <!DOCTYPE html> <html> <head> <meta charse ...