当前位置:网站首页>[142. circular linked list II]
[142. circular linked list II]
2022-06-22 21:06:00 【Cat star people who love Durian】
List of articles
One 、 Title Description
Two 、 How to do it
Train of thought : Formula proof 
Train of thought two : When you don't know the derived formula
You must be in the circle when you meet , It is the tail node
Do it with the idea of intersecting linked lists 
Train of thought three :
3、 ... and 、 Title code
Train of thought : Formula proof
/** * 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;
}
Train of thought two :
/** * 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;
}
Train of thought three :
/** * 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;
}
The above is the whole content of this article , If there are mistakes in the article or something you don't understand , Communicate more with meow bloggers . Learn from each other and make progress . If this article helps you , Can give meow bloggers a concern , Your support is my biggest motivation .
边栏推荐
- 扩展Ribbon支持基于元数据的版本管理
- 87-with as写法的5种用途
- 【513. 找树左下角的值】
- A Dynamic Near-Optimal Algorithm for Online Linear Programming
- Golang learning notes - structure
- C语言中int和char的对应关系
- 极客星球 | 业务监控及告警系统体系化建设之路
- [Jianzhi offer] interview question 44 A digit in a sequence of numbers
- The real king of cache
- 84-我对网传&lt;52 条 SQL 语句性能优化策略&gt;的一些看法
猜你喜欢

软件测试——测试用例设计&测试分类详解

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

53页智慧校园智能化系统设计方案(附下载)

R language organdata dataset visualization

访问成功但抛出异常:Could not find acceptable representation
mysql8.0忘记密码的详细解决方法

Moke 6. Load balancing ribbon

【链表中倒数第k个结点】

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

【21. 合并两个有序链表】
随机推荐
真正的缓存之王Caffine Cache
农产品期货开户
NBA季后赛对阵图
R语言midwest数据集可视化
Visualization of R language nutrient dataset
扩展Ribbon支持Nacos权重的三种方式
Flutter System Architecture(Flutter系统架构图)
Implementation of UART with analog serial port
91-oracle普通表改分区表的几种方法
Kotlin1.6.20 new features context receivers tips
Unityeditor editor script execution menu
laravel+宝塔计划任务
CVPR 2022 oral | video text pre training new SOTA, HKU and Tencent arc lab launched excuse task based on multiple-choice questions
让知识付费系统视频支持M3U8格式播放的方法
PlainSelect.getGroupBy()Lnet/sf/jsqlparser/statement/select/GroupByElement;
[proteus simulation] 8x8LED dot matrix digital cyclic display
72-最近一次现场生产系统优化的成果与开发建议
Performance test (I)
Scheduling with Testing
访问成功但抛出异常:Could not find acceptable representation

