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, 你 ...
随机推荐
- 使用SpringBoot + JavaMailSender 发送邮件报错 Mail server connection failed;Could not connect to SMTP host
说明: 出于安全考虑,阿里云默认封禁 TCP 25 端口出方向的访问流量,无法在阿里云上的云服务器通过 TCP 25 端口连接外部地址. [官方提示]:如果您需要使用阿里云上的云服务器对外部发送邮件, ...
- F-NAScan:一款网络资产扫描工具
此脚本的大概流程为: ICMP存活探测-->端口开放探测-->端口指纹服务识别-->提取快照(若为WEB)-->生成结果报表 用法 python NAScan.py -h 10 ...
- Django 配置使用日志
一. Django中使用日志 Django中使用日志其实非常简单,只需要在项目使用的配置文件中(如果没有自定义,那么就是settings.py中)加以下设置即可,同时可以根据自己的需求进行修改: # ...
- navicat使用、pymysql连接数据库
内容回顾 select distinct 字段1,字段2,... from 表名 where 分组之前的过滤条件 group by 分组条件 having 分组之后过滤条件 order by 排序字段 ...
- [set]Codeforces 830B-Cards Sorting
Cards Sorting time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- 【MySQL】面试官:谈谈你对Mysql的MVCC的理解?
MVCC(Mutil-Version Concurrency Control),就是多版本并发控制.MVCC 是一种并发控制的方法,一般在数据库管理系统中,实现对数据库的并发访问. 在Mysql的In ...
- 原 c++中map与unordered_map的区别
c++中map与unordered_map的区别 头文件 map: #include < map > unordered_map: #include < unordered_map ...
- python之道14
看代码写结果: def wrapper(f): def inner(*args,**kwargs): print(111) ret = f(*args,**kwargs) print(222) ret ...
- 优化Python代码的4种方法
介绍 作为数据科学家,编写优化的Python代码非常非常重要.杂乱,效率低下的代码即浪费你的时间甚至浪费你项目的钱.经验丰富的数据科学家和专业人员都知道,当我们与客户合作时,杂乱的代码是不可接受的. ...
- 关于TensorFlow九件你非知不可的事
来源 | Hackernoon 译者 | Revolver 前些天我参加了7 月24 日在美国旧金山举行的Google Cloud Next 2018 大会,其中的一个演讲( What's New w ...