当前位置:网站首页>[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 .
边栏推荐
猜你喜欢
Code to Image Converter | 代码生成漂亮图片工具

laravel+宝塔计划任务

Xunrui CMS custom data interface PHP executable code

讲真,Kotlin 协程的挂起没那么神秘(原理篇)

Flutter System Architecture(Flutter系统架构图)

CVPR 2022 oral | video text pre training new SOTA, HKU and Tencent arc lab launched excuse task based on multiple-choice questions

Ribbon负载均衡

R 语言 wine 数据集可视化

【文末送书】火遍全网的AI给老照片上色,这里有一份详细教程!
Code to image converter
随机推荐
Notes d'apprentissage de golang - structure
【138. 复制带随机指针的链表】
极客星球 | 业务监控及告警系统体系化建设之路
Visualization of R language penguins dataset
[proteus simulation] H-bridge drive DC motor composed of triode + key forward and reverse control
性能测试(一)
程序员必看的学习网站
74-这类SQL优化,oracle输给了mysql,如何补救?
SwiftUI如何模拟视图发光增大的动画效果
NBA季后赛对阵图
R语言AirPassengers数据集可视化
Easydss problem and solution summary
How to use feign to construct multi parameter requests
苹果Objective-C源代码
Performance test (I)
Moke 6. Load balancing ribbon
【观察】软件行业创新进入“新周期”,如何在变局中开新局?
迅睿CMS 自定义数据接口-php执行文件代码
【21. 合并两个有序链表】
EasyClick 固定状态日志窗口

