leetcode 141. Linked List Cycle 、 142. Linked List Cycle II
判断链表有环,环的入口结点,环的长度
1.判断有环:
快慢指针,一个移动一次,一个移动两次
2.环的入口结点:
相遇的结点不一定是入口节点,所以y表示入口节点到相遇节点的距离
n是环的个数
- w + n + y = 2 (w + y)
经过化简,我们可以得到:w = n - y;
https://www.cnblogs.com/zhuzhenwei918/p/7491892.html
3.环的长度:
从入口结点或者相遇的结点移动到下一次再碰到这个结点计数
https://blog.csdn.net/jyy305/article/details/75267969
141. Linked List Cycle
使用快慢指针,一个一次滑动一次,一个一次滑动两次。
如果只是判断有没有环,初始化可以不两个都初始化到head,但为了方便和找入口节点那个题的记忆,都初始化为head。
两个都初始化为head,就不能先把if(p1 == p2)放在while循环的开始,要放在迭代之后。
class Solution {
public:
bool hasCycle(ListNode *head) {
if(head == NULL)
return false;
//ListNode* p1 = head,p2 = head;
ListNode* p1 = head;
ListNode* p2 = head;
while(p2 != NULL && p2->next != NULL){
p1 = p1->next;
p2 = p2->next->next;
if(p1 == p2)
return true;
}
return false;
}
};
142. Linked List Cycle II
两个指针初始化必须初始化在head的地方,这样才能使用后面推导的公式https://www.cnblogs.com/ymjyqsx/p/9568129.html
在head申明一个新的节点,一个一个滑动,然后p1也是一个一个滑动,相遇的节点就是入口节点
head->next == NULL是为了避免[1]这种情况
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
if(head == NULL)
return NULL;
if(head->next == NULL)
return NULL;
ListNode* p1 = head;
ListNode* p2 = head;
while(p2 != NULL && p2->next != NULL){
p1 = p1->next;
p2 = p2->next->next;
if(p1 == p2)
break;
}
if(p1 != p2){
return NULL;
}
ListNode* p3 = head;
while(p1 != p3){
p1 = p1->next;
p3 = p3->next;
}
return p1;
}
};
leetcode 141. Linked List Cycle 、 142. Linked List Cycle II的更多相关文章
- leetcode 118. Pascal's Triangle 、119. Pascal's Triangle II 、120. Triangle
118. Pascal's Triangle 第一种解法:比较麻烦 https://leetcode.com/problems/pascals-triangle/discuss/166279/cpp- ...
- 【算法分析】如何理解快慢指针?判断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&142 Linked List Cycle I & II
Problem: Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without ...
- (LeetCode 141/142)Linked List Cycle
1.Given a linked list, determine if it has a cycle in it. 2.Given a linked list, return the node whe ...
- <LeetCode OJ> 141 / 142 Linked List Cycle(I / II)
Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using ex ...
- 141. Linked List Cycle&142. Linked List Cycle II(剑指Offer-链表中环的入口节点)
题目: 141.Given a linked list, determine if it has a cycle in it. 142.Given a linked list, return the ...
- [LeetCode] 142. Linked List Cycle II 单链表中的环之二
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To r ...
- [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 ...
随机推荐
- Anychart隐藏属性
一.嵌入字体的使用 font标签可以使用嵌入字体,只需加入embed="true"即可.
- windows 查看端口
windowsnetstat命令查看进程:netstat -ano查看占用端口进程:netstat -ano|findstr “端口号”,例子netstat -ano|findstr “8080”.t ...
- task16 表格增减笔记
trim()方法会创建一个字符串副本,删除前置及后缀所有空格,然后返回结果(中间的空格符无法消除) match()方法可在字符串内检索指定的值,找到一个或多个正则表达式的匹配 正则表达式 匹配中文:[ ...
- HTML利用posotion属性定位 小技巧
1.居中效果 父级DIV (index-top )属性设置为 text-align:center; 子级DIV( tabIndex-main)属性设置为 margin:0 auto; 2.左右对齐 ...
- Ubuntu下彻底卸载wine
简介: wine是linux下模拟windows的一个东西,可以用来安装exe程序,但是对于wine的卸载确 实非常麻烦的,这里是彻底卸载wine的一个教程. ##首先卸载wine sudo apt- ...
- asp:FileUpload 上次图片
<asp:FileUpload ID="FileUpload附件" runat="server" Width="200px" /> ...
- SQLite中7(8)形参的query语句的用法
SQLite中7(8)形参的query语句的用法 我们先来看看这种7形参的query语句的形参列表: public Cursor query(String table, String[] column ...
- Android 获取全局Context的技巧
回想这么久以来我们所学的内容,你会发现有很多地方都需要用到Context,弹出Toast的时候需要.启动活动的时候需要.发送广播的时候需要.操作数据库的时候需要.使用通知的时候需要等等等等.或许目前你 ...
- 创建和修改 ExpressRoute 线路的对等互连
本文将指导你执行相关步骤,以便使用 Azure 门户和 Resource Manager 部署模型创建和管理 ExpressRoute 线路的路由配置. 配置先决条件 在开始配置之前,请务必查看先决条 ...
- 【MySQL】Linux下mysql安装全过程——小白入门篇(含有问题详解)
本次安装操作在申请的腾讯云上实现(版本:CentOS Linux release 7.4.1708 (Core) ). 根据教程实现(中途各种挖坑,填坑...),地址:http://www.runoo ...