142 Linked List Cycle II(如果链表有环,找到入口结点Medium)
题目意思:如果有环,返回入口结点
思路:先判断有没环,再计算环的结点数,然后p1指向头,p2往后移结点次数,p1、p2相遇为入口结点
ps:还是利用指针间距这个思路
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
if(head==NULL)return NULL;
ListNode *p1,*p2;
p1=p2=head;
int n=;
while(p2->next&&p2->next->next){
p1=p1->next;
p2=p2->next->next;
if(p1==p2){
while(p1->next!=p2){
p1=p1->next;
++n;
}
p1=p2=head;
while(n--){
p2=p2->next;
}
while(true){
if(p1==p2)return p1;
p1=p1->next;
p2=p2->next;
}
}
}
return NULL;
}
};
142 Linked List Cycle II(如果链表有环,找到入口结点Medium)的更多相关文章
- [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 ...
- 142 Linked List Cycle II 环形链表 II
给一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null.说明:不应修改给定的链表.补充:你是否可以不用额外空间解决此题?详见:https://leetcode.com/proble ...
- leetcode 142. Linked List Cycle II 环形链表 II
一.题目大意 https://leetcode.cn/problems/linked-list-cycle-ii/ 给定一个链表的头节点 head ,返回链表开始入环的第一个节点. 如果链表无环,则 ...
- 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 141. Linked List Cycle 、 142. Linked List Cycle II
判断链表有环,环的入口结点,环的长度 1.判断有环: 快慢指针,一个移动一次,一个移动两次 2.环的入口结点: 相遇的结点不一定是入口节点,所以y表示入口节点到相遇节点的距离 n是环的个数 w + n ...
- 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现
引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ...
- 142. Linked List Cycle II【easy】
142. Linked List Cycle II[easy] Given a linked list, return the node where the cycle begins. If ther ...
- [PHP] 算法-请找出带环链表的环的入口结点的PHP实现
给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null 1.找链表倒数第k个结点,输入一个链表,输出该链表中倒数第k个结点.第一个指针走(k-1)步,到达第k个节点,两个指针同时往后 ...
- 给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null。
package algorithms; /* 给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null. public class ListNode { int val; ListNo ...
随机推荐
- 【gm】
gm : GraphicsMagick for node.js aheckmann/gm imgAreaSelect 图片剪裁 apt-get install imagemagick 执行conver ...
- ACM生活总结
两年ACM生活总结 转眼已经踏入ACM这条不归路已经两年了, 深深的感觉到ACM的不易 和 艰辛,但同时ACM给我所带来的快乐,让我认为值一切都是值得的. 我刚上大学那会,我们学校的ACM刚刚起步不到 ...
- SQL条件表达式
需求:数据表中如果满足某个条件,返回表中所有数据,否则不返回数据. 语法:CASE 表达式是一种通用的条件表达式,类似于其它语言中的 if/else 语句. CASE WHEN condition T ...
- workflow4.0学习资料
http://www.cnblogs.com/foundation/archive/2010/04/03/1703809.html 2篇说明: WF4 Bata 2 WF4.0 RC 对比 Beta2 ...
- FZYZ-2071 A Simple Math Problem IX
P2071 -- A Simple Math Problem IX 时间限制:1000MS 内存限制:262144KB 状态:Accepted 标签: 数学问题-博弈论 ...
- 2012 B 中国近现代史纲要》课程期末考试试卷
湖南人文科技学院2013年3月公共课 2011级<中国近现代史纲要>课程期末考试试卷B 考核方式:(开卷) 考试时量: ...
- SoupUI学习资料
官网: https://www.soapui.org 下载地址: https://www.soapui.org/downloads/soapui.html 官方文档: https://www.soap ...
- JavaScript中bind、call、apply函数用法详解
在给我们项目组的其他程序介绍 js 的时候,我准备了很多的内容,但看起来效果不大,果然光讲还是不行的,必须动手.前几天有人问我关于代码里 call() 函数的用法,我让他去看书,这里推荐用js 写服务 ...
- 数据库框架 Litepal
1.导包 dependencies { compile 'org.litepal.android:core:1.4.1' } 2.在asstes中建立litepal.xml文件 <?xml ...
- Android--图片的三级缓存策略
三级缓存缓存(内存)--->本地磁盘---->网络1.首先看一下图片存储到本地磁盘 public class FileUtils { String path;//文件存储的地方 publi ...