当前位置:网站首页>leetcode 剑指 Offer 52. 两个链表的第一个公共节点
leetcode 剑指 Offer 52. 两个链表的第一个公共节点
2022-07-30 08:52:00 【kt1776133839】
题目描述:
输入两个链表,找出它们的第一个公共节点。
如下面的两个链表:

在节点 c1 开始相交。
样例:
示例 1:

输入:intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
输出:Reference of the node with value = 8
输入解释:相交节点的值为 8 (注意,如果两个列表相交则不能为 0)。从各自的表头开始算起,链表 A 为 [4,1,8,4,5],链表 B 为 [5,0,1,8,4,5]。在 A 中,相交节点前有 2 个节点;在 B 中,相交节点前有 3 个节点。
示例 2:

输入:intersectVal = 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
输出:Reference of the node with value = 2
输入解释:相交节点的值为 2 (注意,如果两个列表相交则不能为 0)。从各自的表头开始算起,链表 A 为 [0,9,1,2,4],链表 B 为 [3,2,4]。在 A 中,相交节点前有 3 个节点;在 B 中,相交节点前有 1 个节点。
示例 3:

输入:intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
输出:null
输入解释:从各自的表头开始算起,链表 A 为 [2,6,4],链表 B 为 [1,5]。由于这两个链表不相交,所以 intersectVal 必须为 0,而 skipA 和 skipB 可以是任意值。
解释:这两个链表不相交,因此返回 null。
注意:
如果两个链表没有交点,返回 null.
在返回结果后,两个链表仍须保持原有的结构。
可假定整个链表结构中没有循环。
程序尽量满足 O(n) 时间复杂度,且仅用 O(1) 内存。
解题思路:
双指针
使用双指针的方法,可以将空间复杂度降至 O(1)。
只有当链表 headA 和 headB 都不为空时,两个链表才可能相交。因此首先判断链表 headA 和 headB 是否为空,如果其中至少有一个链表为空,则两个链表一定不相交,返回 null。
当链表 headA 和 headB 都不为空时,创建两个指针 pA 和 pB,初始时分别指向两个链表的头节点 headA和 headB,然后将两个指针依次遍历两个链表的每个节点。具体做法如下:
每步操作需要同时更新指针 pA 和 pB。
如果指针 pA 不为空,则将指针 pA 移到下一个节点;如果指针 pB 不为空,则将指针 pB 移到下一个节点。
如果指针 pA 为空,则将指针 pA 移到链表 headB 的头节点;如果指针 pB 为空,则将指针 pB 移到链表 headA 的头节点。
当指针 pA 和 pB 指向同一个节点或者都为空时,返回它们指向的节点或者 null。
Java程序:
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if (headA == null || headB == null) {
return null;
}
ListNode pA = headA, pB = headB;
while (pA != pB) {
pA = pA == null ? headB : pA.next;
pB = pB == null ? headA : pB.next;
}
return pA;
}
}
边栏推荐
- 激活数据潜力 亚马逊云科技重塑云上存储“全家桶”
- 一文读懂二十种开关电源拓扑结构
- 怎么在本地电脑上运行dist文件
- Kotlin 值类 - value class
- How to implement Golang DES encryption and decryption?
- 功能测试、UI自动化测试(web自动化测试)、接口自动化测试
- 2022/07/29 学习笔记 (day19)异常处理
- Two solutions for Excel xlsx file not supported
- Integral Special Notes - Definition of Integral
- ACL 2022 | Introduce angular margin to construct comparative learning objectives and enhance text semantic discrimination ability
猜你喜欢

Leetcode - 990: equations of satisfiability

SRAM与DRAM的区别

一文读懂二十种开关电源拓扑结构

嘉为鲸翼·多云管理平台荣获信通院可信云技术服务最佳实践

20220728 Use the bluetooth on the computer and the bluetooth module HC-05 of Huicheng Technology to pair the bluetooth serial port transmission

Explain the problem of change exchange in simple terms - the shell of the backpack problem

如何避免CMDB沦为数据孤岛?

Access to display the data

实施敏捷过程中这些常见的问题你可曾遇到?

新手必备!最全电路基础知识讲解
随机推荐
20个电路能懂5个以上,足以证明你在电子行业混过!
聊聊 MySQL 事务二阶段提交
自动化测试selenium(一)
SRAM与DRAM的区别
Excel xlsx file not supported两种解决办法【杭州多测师】【杭州多测师_王sir】
XP电源维修fleXPower电源X7-2J2J2P-120018系列详解
利用R语言读取csv文件入一个数据框,然后查看各列的属性。
Using IN in MySQL will not go through index analysis and solutions
2022杭电多校第二场
Network/Information Security Top Journal and Related Journals Conference
转行软件测试,报培训班3个月出来就是高薪工作,靠谱吗?
PyTorch安装及环境配置(Win10)
如何避免CMDB沦为数据孤岛?
【愚公系列】2022年07月 Go教学课程 021-Go容器之切片操作
函数式接口&Lambda表达式——简单应用笔记
How to Assemble a Registry
The difference between DDR, GDDR, QDR
pnpm简介
知识图谱之Cypher语言的使用
MySQL【运算符】