alg-链表中有环
typedef struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(nullptr) {}
}ListNode;
class Solution {
public:
//链表中是否有环
bool hasCycle(ListNode* head) {
if(!head) {
return false;
}
ListNode* fast=head;
ListNode* slow=head;
while(fast&&fast->next) {
slow=slow->next;
fast=fast->next->next;
if(fast==slow) {
return true;
}
}
return false;
}
//链表中环的起始点
ListNode *beginCycle(ListNode* head) {
if(!head) return nullptr;
ListNode *fast = head;
ListNode *slow = head;
while(fast&& fast->next) {
fast = fast->next->next;
slow = slow->next;
if(fast == slow) {
//从相遇点和链表起始点同时循环两个指针,直到二者相遇,相遇点就是环起始点
ListNode *first = head;
while(first != slow) {
first = first->next;
slow = slow->next;
}
return first;
}
}
return NULL;
}
//链表中环的长度
int lengthCycle(ListNode* head) {
if(!head) {
return false;
}
ListNode* fast=head;
ListNode* slow=head;
while(fast&&fast->next) {
slow=slow->next;
fast=fast->next->next;
if(fast==slow) {
slow=slow->next;
int length=1;
while(slow!=fast) {
slow=slow->next;
length++;
}
return length;
}
}
return 0;
}
};
void TestListCycle() {
ListNode lp1(1);
ListNode lp2(2);
ListNode lp3(3);
ListNode lp4(4);
ListNode lp5(5);
ListNode lp6(6);
ListNode lp7(7);
ListNode lp8(8);
ListNode lp9(9);
lp1.next=&lp2;
lp2.next=&lp3;
lp3.next=&lp4;
lp4.next=&lp5;
lp5.next=&lp6;
lp6.next=&lp7;
lp7.next=&lp8;
lp8.next=&lp9;
lp9.next=&lp3;
ListNode* head=&lp1;
Solution s;
std::cout<<"hasCycle:"<<s.hasCycle(head)<<std::endl;
ListNode* lp=nullptr;
lp=s.beginCycle(head);
std::cout<<"detectCycle:"<<lp->val<<std::endl;
std::cout<<"lengthCycle:"<<s.lengthCycle(head)<<std::endl;
}
alg-链表中有环的更多相关文章
- 【C++】链表回环检测
//链表回环检测问题 #include<iostream> #include<cstdlib> using namespace std; ; struct node { int ...
- [PHP] 算法-请找出带环链表的环的入口结点的PHP实现
给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null 1.找链表倒数第k个结点,输入一个链表,输出该链表中倒数第k个结点.第一个指针走(k-1)步,到达第k个节点,两个指针同时往后 ...
- 《剑指offer》-链表找环入口
题目描述 一个链表中包含环,请找出该链表的环的入口结点. 初步想法是每个节点做几个标记,表示是否被访问过,那么遍历链表的时候就知道哪个被访问到了.但是不会实现. 另一个直觉是判断链表有环的算法中出现过 ...
- Linked List Cycle leetcode java (链表检测环)
题目: Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without usin ...
- 给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null。
package algorithms; /* 给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null. public class ListNode { int val; ListNo ...
- 转 java - 如何判断单链表有环
转自 https://blog.csdn.net/u010983881/article/details/78896293 1.穷举遍历 首先从头节点开始,依次遍历单链表的每一个节点.每遍历到一个新节点 ...
- select 函数实现 三种拓扑结构 n个客户端的异步通信 (完全图+线性链表+无环图)
一.这里只介绍简单的三个客户端异步通信(完全图拓扑结构) //建立管道 mkfifo open顺序: cl1 读 , cl2 cl3 向 cl1写 cl2 读 , cl1 cl3 向 cl2写 cl3 ...
- leetCodelinked-list-cycle-ii找到链表的环
题目 Given a linked list, return the node where the cycle begins. If there is no cycle, return null. N ...
- leetcode - 链表两两元素交换 + 判断链表有无环
链表两两元素交换 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换. 示例: 给定 1->2->3->4, 你 ...
随机推荐
- java-FileUtils(复制文件夹、复制文件、字符串直接写入文件中)(新手)
实例: lx1: import org.apache.commons.io.FileUtils; import java.io.File; import java.io.IOException; pu ...
- 【简说Python WEB】flask-mail电子邮件异步Asynchronous
系统环境:Ubuntu 18.04.1 LTS Python使用的是虚拟环境:virutalenv Python的版本:Python 3.6.9 flask-mail电子邮件异步Asynchronou ...
- C语言之歌词解析
0x00 脚下的路 不知道为啥要写这个小标题,可能是年轻的心想体验一下苍老的感觉,抑或是少年的一阵迷茫.混沌的四年,终究还是入了这一行.从初时的不知,到现在的刚开始,中间的间隔竟是四年之久,想起了陈奕 ...
- 新文预览 | IoU-aware Single-stage Object Detector for Accurate Localization
论文基于RetinaNet提出了IoU-aware sinage-stage目标检测算法,该算法在regression branch接入IoU predictor head并通过加权分类置信度和IoU ...
- 目标检测 | RetinaNet:Focal Loss for Dense Object Detection
论文分析了one-stage网络训练存在的类别不平衡问题,提出能根据loss大小自动调节权重的focal loss,使得模型的训练更专注于困难样本.同时,基于FPN设计了RetinaNet,在精度和速 ...
- python爬取网站页面时,部分标签无指定属性而报错
在写爬取页面a标签下href属性的时候,有这样一个问题,如果a标签下没有href这个属性则会报错,如下: 百度了有师傅用正则匹配的,方法感觉都不怎么好,查了BeautifulSoup的官方文档,发现一 ...
- 关于 word2vec 如何工作的问题
2019-09-07 22:36:21 问题描述:word2vec是如何工作的? 问题求解: 谷歌在2013年提出的word2vec是目前最常用的词嵌入模型之一.word2vec实际是一种浅层的神经网 ...
- 大规模机器学习(Large Scale Machine Learning)
本博客是针对Andrew Ng在Coursera上的machine learning课程的学习笔记. 目录 在大数据集上进行学习(Learning with Large Data Sets) 随机梯度 ...
- TensorFlow v2.0实现Word2Vec算法
使用TensorFlow v2.0实现Word2Vec算法计算单词的向量表示,这个例子是使用一小部分维基百科文章来训练的. 更多信息请查看论文: Mikolov, Tomas et al. " ...
- 关于用命令行和idea对项目打jar包
前提说一下,我们一般是对编译后的项目进行打包,不然打包后还得自己去重新编译class文件. 假如这是你的一个项目目录: 我们要写一个简单的计算器工具类项目,然后对他进行打包, idea里面out文件夹 ...