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 ...
随机推荐
- Linux Shell编程(3)——运行shell脚本
写完一个脚本,你能够运行它用命令:sh scriptname, [5] 另外也也可以用bash scriptname. 来执行(不推荐使用:sh <scriptname, 因为这样会禁止脚本从标 ...
- Codeforces 716B Complete the Word【模拟】 (Codeforces Round #372 (Div. 2))
B. Complete the Word time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- ACM第六周竞赛题目——B CodeForces 478B
B - B Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Submit Statu ...
- python 几种常见的测试框架
1. unittest 参考文档: https://docs.python.org/3/library/unittest.html The unittest unit testing framewor ...
- 谈谈C#中的接口
接口的相关陈述 1.一个接口定义了一个契约. 2.接口可以包容方法.C#属性.事件.以及索引器. 3.在一个接口声明中,我们可以声明零个或者多个成员. 4.所有接口成员的默认访问类型都是public. ...
- dubbo简述
转http://blog.csdn.net/hzzhoushaoyu/article/details/4327309 一.前言 部门去年年中开始各种改造,第一步是模块服务化,这边初选dubbo试用在一 ...
- VMware vSphere 服务器虚拟化之二十五 桌面虚拟化之终端服务池
VMware vSphere 服务器虚拟化之二十五 桌面虚拟化之终端服务池 终端服务池是指由一台或多台微软终端服务器提供服务的桌面源组成的池.终端服务器桌面源可交付多个桌面.它具有以下特征: 1.终端 ...
- Linux TCP队列相关参数的总结
作者:阿里技术保障锋寒 原文:https://yq.aliyun.com/articles/4252 摘要: 本文尝试总结TCP队列缓冲相关的内核参数,从协议栈的角度梳理它们,希望可以更容易的理解和记 ...
- Meth | phpstorm 2016.2 的最新破解方法(截止2016-8-1)
今天刚更新了phpstorm 2016.2版本,发现网上提供的破解地址都有问题,即*.lanyus.com及*.qinxi1992.cn下的全部授权服务器已遭JetBrains封杀. 最后网上找到一个 ...
- Java基础知识强化之IO流笔记14:递归之输出指定目录下所有java文件绝对路径的案例
1. 需求:输出指定目录下的所以.java结尾文件的绝对路径的案例: 分析: A:封装目录 B:获取该目录下的所有文件和文件夹的File数组 C:遍历这个File数组,得到每一个File对象的 ...