当前位置:网站首页>[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 .
边栏推荐
猜你喜欢
随机推荐
Golang learning notes - structure
【观察】软件行业创新进入“新周期”,如何在变局中开新局?
Ribbon负载均衡
采用网络远程访问树莓派。
【876. 链表的中间结点】
一行代码为特定状态绑定SwiftUI视图动画
评估指标及代码实现(NDCG)
深度学习常用损失函数总览:基本形式、原理、特点
Résolu: peut - on avoir plus d'une colonne auto - incrémentale dans un tableau
79-不要看到有order by xxx desc就创建desc降序索引-文末有赠书福利
85-这些SQL调优小'技巧',你学废了吗?
启牛送的券商账户是安全的吗?启牛提供的券商账户是真的?
R语言penguins数据集可视化
Huawei cloud releases Latin American Internet strategy
Emotion analysis with RNN & CNN pytorch
Unityeditor editor script execution menu
Notes d'apprentissage de golang - structure
MySQL advanced (II)
2022团体程序设计天梯赛L1
R language universalbank CSV "data analysis










