[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 ...
随机推荐
- 团队作业第三次 —— UML设计
这个作业要求在哪里 https://edu.cnblogs.com/campus/fzzcxy/2018SE2/homework/11366 这个作业的目标 <团队一起设计UML图> 团队 ...
- Docker 指南
一.docker 介绍 1.1 引言 环境不一致 "我本地运行没问题啊?!" 多用户相互影响 "哪个哥们又写死循环了,怎么这么卡?!" 运维成本高 " ...
- cert-manager管理内网k8s开发环境证书
目的 内网k8s开发环境配置HTTPS,保持与生产环境的配置的一致性,其必要性有: PWA开发,HTTPS是必要条件 网页引入HTTP资源,如果开发环境是HTTP就不会被开发和测试人员发现,造成生产环 ...
- 20191017_datatable.select() 数据源中没有dataRow
filterStr =" 记录时间 >= '2019/10/17 00:00:00' and 记录时间 <='2019/10/20 23:59:59' " 代码: dg ...
- R语言无网络安装R包,彻底解决依赖问题!
R version: 3.5.3, 3.6.3 更新日期: 2020-9-10 大家测试后多提建议哈, 有问题我会持续更新的 在工作中,我们使用的服务器通常是不能联外网的,这在安装R包的时候产生了巨大 ...
- 区块链学习7:超级账本项目Hyperledger与Fabric以及二者的关系
☞ ░ 前往老猿Python博文目录 ░ 一.超级账本(hyperledger) 超级账本(hyperledger)是Linux基金会于2015年发起的推进区块链数字技术和交易验证的开源项目,成员包括 ...
- 第11.7节 Python正则表达式的字符串结尾匹配模式及元字符“$”功能介绍
符号"$"表示匹配字符串的结尾,即字符串的结尾满足匹配模式的要求. 在 MULTILINE 模式(搜索标记中包含re.MULTILINE,关于搜索标记的含义请见<第11.2节 ...
- LeetCode初级算法之数组:217 存在重复元素
存在重复元素 题目地址:https://leetcode-cn.com/problems/contains-duplicate/ 给定一个整数数组,判断是否存在重复元素.如果任意一值在数组中出现至少两 ...
- 利用IDEA把Java项目打成jar包
第一步:按如下步骤或Ctrl+Shift+Alt+S打开 Project Structure第二步:第三步:选择要执行的文件, 依次选择项目, main方法所在的文件, 保存如果出现以下错误:则根据 ...
- 剑指offer二刷——数组专题——数组中出现次数超过一半的数字
题目描述 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2. ...