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

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. js-NodeList对象和HTMLCollection对象

    getElementsByName()和getElementsByTagName()都返回NodeList对象,而类似document.images和document.forms的属性为HTMLCol ...

  2. sql_date

    往Oracle数据库中插入日期型数据(to_date的用法) INSERT  INTO  FLOOR  VALUES  ( to_date ( '2007-12-20 18:31:34' , 'YYY ...

  3. [移动端WEB] 移动端网站响应式布局之"rem",CSS3 rem使用方式

    (function(doc, win) { var docEl = doc.documentElement, resizeEvt = 'orientationchange' in window ? ' ...

  4. <Android 基础(二十六)> 渐变色圆角Button

    简介 总结下之前看的自定义View的内容,结合一个简单的例子,阐述下基本用法和大致的使用流程,这个例子比较简单,更复杂的自定义View,随着自己的学习,后面再慢慢添加.作为一个Android开发者,这 ...

  5. 2018-10-13 21:30:51 conversion of number systems

    2018-10-13 21:30:51  c language 二进制.八进制和十六进制: 1) 整数部分 十进制整数转换为 N 进制整数采用“除 N 取余,逆序排列”法. 十进制数字 36926 转 ...

  6. 612.1.003 ALGS4 | Stacks and Queues

    Algorithm | Coursera - by Robert Sedgewick Type the code one by one! 不要拜读--只写最有感触的!而不是仅仅做一个笔记摘录员,那样毫 ...

  7. js 和springboot内存图

  8. CSS深入理解之absolute(HTML/CSS)

    absolute和float是同父异母的兄弟,因为它们具有相同点:包裹性与破坏性 absolute的特点 1.独立的,并且可以摆脱overflow的限制,无论是滚动还是隐藏: 2.无依赖,不受rela ...

  9. 传递命令行参数示例代码 (C 和 Python)

    C语言 在 C 语言中, 使用 main 函数的输入参数 argc 和 argv 传入命令行参数. argc 为 int 类型, 表示传入命令行参数的个数 (argument count); argv ...

  10. 5.String StringBuffer StringBuilder

    String,StringBuffer和StringBuilder三者的讲解 对于StringBuffer和StringBuilder是对Stirng的一个优化. 之前已经说过了,String对象一旦 ...