当前位置:网站首页>两个链表的第一个公共节点_链表中环的入口(剑指offer)
两个链表的第一个公共节点_链表中环的入口(剑指offer)
2022-06-24 06:42:00 【bug 郭】
两个链表的第一个公共结点
- 首先我们想到的就是先让一个链表先遍历差值个单位节点,当两个链表长度相等时,同时遍历如果有相等节点就是有公共节点!
//先计算长度差,然后让一个指针先走差值单位!
public class Solution {
public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
ListNode cur1 = pHead1;
ListNode cur2 = pHead2;
int size1 = 0;
int size2 = 0;
while(cur1!=null){
cur1 = cur1.next;
size1++;
}
while(cur2!=null){
cur2 = cur2.next;
size2++;
}
if(size1>size2){
int len = size1 - size2;
while(len-->0){
pHead1 = pHead1.next;
}
}else{
int len = size2 - size1;
while(len-->0){
pHead2 = pHead2.next;
}
}
while(pHead1!=null){
if(pHead1.val==pHead2.val){
return pHead1;
}
pHead1 = pHead1.next;
pHead2 = pHead2.next;
}
return null;
}
}
- 上面的方法就是借助了路程相等,相同速度如果有公共节点肯定会相遇,我们可以将两个链表加起来,就是一个链表遍历结束,就从另一个链表的开始遍历,另一链表也是如此,就保证了长度相等,就和上面方法类似了,看下面动图分析!

- 通过两个指针速度相同,走过的路程相同必会相遇!
- cur1 走完 L1,cur1指向 L2,cur2走完L2,指向L1!
public class Solution {
public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
//定义2个指针!
// cur1 走完 L1,又从 L2开始!
// cur2 走完 L2,又从 L1开始!
// 这里两个指针速度相同,走过的长度相等,如果有相同节点肯定相遇!
ListNode cur1 = pHead1;
ListNode cur2 = pHead2;
while(cur1!=cur2){
//不存在公共节点,两个指针会来到null相等退出循环!
cur1 = (cur1==null) ? pHead2 : cur1.next;
cur2 = (cur2 == null) ? pHead1 : cur2.next;
}
return cur1;
}
}
链表中环的入口结点

- 这里通过定义两个指针,
slow走一步,fast走2步,如果有环肯定会相遇! - 快慢指针,这里通过 时间相同,
fast指针速度是slow指针的2倍,那么路程也是2倍关系! - 通过位移关系找出了,入口和相遇点的关系,
L = C-x开始节点到入口的距离等于相遇点到环的距离 - 那么我们先通过快慢指针找到相遇点,然后再走
L单位就是入口节点位置!

public ListNode EntryNodeOfLoop(ListNode pHead) {
//快慢指针,利用链表头到入口距离 = 相遇点到入口距离!
//所以当两个节点相遇后再走L距离就是入口位置!
//相遇后让其中一个指针从链头开始走L,一个从相遇点开始!
ListNode slow = pHead,fast = pHead;
while(fast!=null&&fast.next!=null){
//注意判断条件!!!!
fast = fast.next.next;
slow = slow.next;
if(fast==slow){
//相遇!
//让slow从头结点开始!
slow = pHead;
while(fast!=slow){
slow = slow.next;
fast = fast.next;
}
return fast;
}
}
return null;
}
- 注意,这里的快慢指针,只能是一个走1步,一个走2步!其他关系可能无法保证相遇!
边栏推荐
- 关于取模数据序号定位的说明 区码定位是指GBK编码
- 应用配置管理,基础原理分析
- [binary tree] - middle order traversal of binary tree
- How do I check the IP address? What is an IP address
- 华为云数据库进阶学习
- Spark累加器和廣播變量
- 内网学习笔记(4)
- NVIDIA control panel does not open what is NVIDIA control panel
- How to make a website? What should I pay attention to when making a website?
- Vmware tools still exist after normal uninstallation for many times. How to solve it
猜你喜欢

Outils de débogage JVM - Arthas

应用配置管理,基础原理分析

开源与创新

Huawei cloud image engine service

Computing power and intelligence of robot fog

Spark project Packaging Optimization Practice

潞晨科技获邀加入NVIDIA初创加速计划

如何删除/选择电脑上的输入法

Application of intelligent reservoir management based on 3D GIS system

In JS, the regular expression verifies the hour and minute, and converts the input string to the corresponding hour and minute
随机推荐
Typora charges? Build vs Code markdown writing environment
Hyperledger fabric ledger snapshot - fast data synchronization
華為雲數據庫進階學習
EasyDSS_ The dash version solves the problem that the RTSP source address cannot play the video stream
You have a chance, here is a stage
数据同步工具 DataX 已经正式支持读写 TDengine
Spark项目打包优化实践
Win11分磁盘怎么分?Win11系统怎么分磁盘?
Overview of cloud computing advantages of using cloud computing
Why use lock [readonly] object? Why not lock (this)?
Win11笔记本省电模式怎么开启?Win11电脑节电模式打开方法
Challenges brought by maker education to teacher development
App management platform app host
Interpreting top-level design of AI robot industry development
What is the role of domain name websites? How to query domain name websites
Intelligent Vision Group A4 paper recognition example
内网学习笔记(4)
Huawei cloud image engine service
0 foundation a literature club low code development member management applet (III)
如何删除/选择电脑上的输入法