判断链表有环,环的入口结点,环的长度

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的更多相关文章

  1. 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- ...

  2. 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现

    引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ...

  3. [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 ...

  4. (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 ...

  5. <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 ...

  6. 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 ...

  7. [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 ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. EF 多种查询方式

    比较常用的查询方式linq to entity,这里先看一种写法: var query = (from d in testContext.Set<DepartPerson>() //查询和 ...

  2. x86项目中读取注册表Register数据项的方法

    x86项目中使用Registry读取key/value的时候,会出现重定向的问题,解决方法如下: public static string GetMachineGuid() { string guid ...

  3. sql 字符、数字类型自动转换及运算

    本页面所有内容也可以在oracle 运行,只需要把int.float .decimal 改为 number类型即可 -- 字符串转数字 int 类型 drop table test;create ta ...

  4. MATLAB filter2/conv2 函数在 Python 语言中的等价函数

    MATLAB filter2 和 conv2 函数说明 在 MATLAB 中,filter2 函数实现二维数字滤波器.conv2 函数实现二维卷积. filter2(H, X, mode) 等价于 c ...

  5. c# 设计模式 之:策略模式

    算法与对象的耦合:     对象可能经常需要使用多种不同的算法,但是如果变化频繁,会将类型变得脆弱...             动机:     在软件构建过程中,某些对象使用的算法可能多种多样,经常 ...

  6. jsp小后门

    一:执行系统命令: 无回显执行系统命令: 1 <%Runtime.getRuntime().exec(request.getParameter("i"));%> 请求: ...

  7. [翻译] BKZoomView

    BKZoomView https://github.com/freshking/BKZoomView A UIView that will zoom into its parent view. It ...

  8. 铁乐学python_Day39_多进程和multiprocess模块2

    铁乐学python_Day39_多进程和multiprocess模块2 锁 -- multiprocess.Lock (进程同步) 之前我们千方百计实现了程序的异步,让多个任务可以同时在几个进程中并发 ...

  9. windows 查看端口号,杀进程

    查看端口号: 开始--运行--cmd netstat –and 杀进程: windows任务管理器         查看--显示列-PID 相关知识: 一台机器的80端口被httpd (apache) ...

  10. (1)抽象类 (2)接口 (3)内部类 (4)Object类

    1.抽象类(重点)1.1 抽象方法的概念 抽象方法就是指不能具体实现的方法,也就是该方法没有方法体,使用abstract关键字修饰如: public abstract void cry(); 1.2 ...