当前位置:网站首页>【142. 环形链表 II】
【142. 环形链表 II】
2022-06-22 19:41:00 【爱吃榴莲的喵星人】
一、题目描述
二、做题思路
思路一:公式证明
思路二:当不知道推导的公式时
相遇时一定在圈里,它是尾节点
以相交链表的思想去做
思路三:
三、题目代码
思路一:公式证明
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */
struct ListNode *detectCycle(struct ListNode *head) {
struct ListNode * fast = head;
struct ListNode * slow = head;
struct ListNode * meet = NULL;
while(fast && fast->next)
{
fast=fast->next->next;
slow=slow->next;
if(fast == slow)
{
meet = fast;
while(head != meet)
{
head=head->next;
meet=meet->next;
}
return meet;
}
}
return NULL;
}
思路二:
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB,struct ListNode *tail) {
struct ListNode * listA = headA;
struct ListNode * listB = headB;
int lenA=1;int lenB=1;
while(listA!=tail)
{
listA=listA->next;
lenA++;
}
while(listB!=tail)
{
listB=listB->next;
lenB++;
}
if(listA != listB)
{
return NULL;
}
int gap=abs(lenA-lenB);
struct ListNode * longlist = headA;
struct ListNode * shortlist = headB;
if(lenA < lenB)
{
longlist = headB;
shortlist = headA;
}
while(gap--)
{
longlist=longlist->next;
}
while(longlist != shortlist)
{
longlist=longlist->next;
shortlist=shortlist->next;
}
return shortlist;
}
struct ListNode *detectCycle(struct ListNode *head) {
struct ListNode * fast = head;
struct ListNode * slow = head;
struct ListNode * meet = NULL;
while(fast && fast->next)
{
slow=slow->next;
fast=fast->next->next;
if(fast==slow)
{
meet=slow;
struct ListNode * newhead = meet->next;
struct ListNode * p=getIntersectionNode(newhead,head,meet);
return p;
}
}
return NULL;
}
思路三:
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
struct ListNode * listA = headA;
struct ListNode * listB = headB;
int lenA=1;int lenB=1;
while(listA->next)
{
listA=listA->next;
lenA++;
}
while(listB->next)
{
listB=listB->next;
lenB++;
}
if(listA != listB)
{
return NULL;
}
int gap=abs(lenA-lenB);
struct ListNode * longlist = headA;
struct ListNode * shortlist = headB;
if(lenA < lenB)
{
longlist = headB;
shortlist = headA;
}
while(gap--)
{
longlist=longlist->next;
}
while(longlist != shortlist)
{
longlist=longlist->next;
shortlist=shortlist->next;
}
return shortlist;
}
struct ListNode *detectCycle(struct ListNode *head) {
struct ListNode * fast = head;
struct ListNode * slow = head;
struct ListNode * meet = NULL;
while(fast && fast->next)
{
slow=slow->next;
fast=fast->next->next;
if(fast==slow)
{
meet=slow;
struct ListNode * newhead = meet->next;
meet->next=NULL;
struct ListNode * p=getIntersectionNode(newhead,head);
return p;
}
}
return NULL;
}
以上是本篇文章的全部内容,如果文章有错误或者有看不懂的地方,多和喵博主交流。互相学习互相进步。如果这篇文章对你有帮助,可以给喵博主一个关注,你们的支持是我最大的动力。
边栏推荐
- Oh, my God, it's a counter attack by eight part essay
- R语言AirPassengers数据集可视化
- The road to systematic construction of geek planet business monitoring and alarm system
- 让知识付费系统视频支持M3U8格式播放的方法
- [proteus simulation] H-bridge drive DC motor composed of triode + key forward and reverse control
- CVPR 2022 Oral | 视频文本预训练新SOTA,港大、腾讯ARC Lab推出基于多项选择题的借口任务
- SwiftUI如何模拟视图发光增大的动画效果
- 85-这些SQL调优小'技巧',你学废了吗?
- 已解决:一個錶中可以有多個自增列嗎
- 【观察】软件行业创新进入“新周期”,如何在变局中开新局?
猜你喜欢
A detailed solution to mysql8.0 forgetting password

慕课6、实现负载均衡-Ribbon

Xunrui CMS custom data interface PHP executable code

He was in '98. I can't play with him

阿里云视频点播播放出错,控制台访问出现code:4400

Visualization of R language nutrient dataset

Oh, my God, it's a counter attack by eight part essay

The access succeeds but an exception is thrown: could not find acceptable representation

R语言midwest数据集可视化

MySQL Basics - functions
随机推荐
Introduction to async profiler
AAAI 2022 | 传统GAN修改后可解释,并保证卷积核可解释性和生成图像真实性
Kotlin1.6.20 new features context receivers tips
如何使用Feign构造多参数的请求
CVPR 2022 oral | video text pre training new SOTA, HKU and Tencent arc lab launched excuse task based on multiple-choice questions
Performance test (I)
75-当left join遇到子查询
模拟串口UART的实现
性能测试(一)
Visualization of R language penguins dataset
92-几个用match_recognize SQL写法示例
密码学系列之:PKI的证书格式表示X.509
Scheduling with Testing
MYSQL 几个常用命令使用
A Dynamic Near-Optimal Algorithm for Online Linear Programming
R 语言 wine 数据集可视化
Visualization of wine datasets in R language
底部菜单添加的链接无法跳转到二级页面的问题
what? You can't be separated by wechat
How to realize @ person function in IM instant messaging

