[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 ...
随机推荐
- IEEE754标准浮点数表示与舍入
原文地址:https://blog.fanscore.cn/p/26/ 友情提示:本文排版不太好,但内容简单,请耐心观看,总会搞懂的. 1. 定点数 对于一个无符号二进制小数,例如101.111,如果 ...
- Django匆匆一眼却解答了多年疑惑
Django 是 Python 的 一款 Web 开发框架,另外还有 Tornado,Flask,Twisted.为什么我要选择学 Django?原因很简单,上家公司来了个网易的测开,就是用 Djan ...
- JZOJ 11.14 提高B组反思
JZOJ 11.14 提高B组反思 T1 题目虽然有点高大上,但是很容易懂 有一个\(d\)维空间,同时有一个长度为\(2n\)的操作序列,每个操作往某一维的正方向或反方向走一格,问多少种方案使得最后 ...
- go语言的指针类型
一.指针与引用的相关概念 什么是指针? 指针,全称为指针变量,是用来存储内存地址的一种变量.程序中,一般通过指针来访问其指向的内存地址中的内容(数据). 什么是引用? 引用,是C++中提出来的一种新的 ...
- java并发编程实战《二十一》无锁工具类
不安全的累加代码,如下 1 public class Test { 2 long count = 0; 3 void add10K() { 4 int idx = 0; 5 while(idx++ & ...
- moviepy简介及安装
专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 老猿学5G博文目录 一.概述 MoviePy是一个用于视频编辑的Pyt ...
- 第15.37节 PyQt(Python+Qt)入门学习:containers容器类部件QMdiArea多文档界面部件详解及编程开发案例
专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 一.引言 老猿在前期学习PyQt相关知识时,对每个组件的属性及方法都研 ...
- Python正则表达式re模块学习遇到的问题
Python正则表达式处理的组是什么? Python正则表达式处理中的匹配对象是什么? Python匹配对象的groups.groupdict和group之间的关系 Python正则表达式re.mat ...
- Alpha冲刺阶段Day4
[Alpha冲刺阶段]Scrum Meeting Daily4 1.会议简述 会议开展时间 2020/5/25 7:30-7:50 PM 会议基本内容摘要 讨论了各自任务完成情况以及明日计划 参与 ...
- 状压DP复习笔记
前言 复习笔记第4篇.CSP RP++. 引用部分为总结性内容. 0--P1433 吃奶酪 题目链接 luogu 题意 房间里放着 \(n\) 块奶酪,要把它们都吃掉,问至少要跑多少距离?一开始在 \ ...