当前位置:网站首页>链表——剑指offer面试题 02.07. 链表相交
链表——剑指offer面试题 02.07. 链表相交
2022-07-24 11:23:00 【向着百万年薪努力的小赵】
1 题目描述
面试题 02.07. 链表相交
给你两个单链表的头节点 headA 和 headB ,请你找出并返回两个单链表相交的起始节点。如果两个链表没有交点,返回 null 。
图示两个链表在节点 c1 开始相交:
题目数据 保证 整个链式结构中不存在环。
注意,函数返回结果后,链表必须 保持其原始结构 。
2 题目示例

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

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

输入: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 。
3 题目提示
- listA 中节点数目为 m
- listB 中节点数目为 n
- 0 <= m, n <= 3 * 104
- 1 <= Node.val <= 105
- 0 <= skipA <= m
- 0 <= skipB <= n
- 如果 listA 和 listB 没有交点,intersectVal 为 0
- 如果 listA 和 listB 有交点,intersectVal == listA[skipA + 1] == listB[skipB + 1]
4 思路
方法一:哈希集合
判断两个链表是否相交,可以使用哈希集合存储链表节点。
首先遍历链表 headA,并将链表 headA 中的每个节点加入哈希集合中。然后遍历链表 headB,对于遍历到的每个节点,判断该节点是否在哈希集合中:
如果当前节点不在哈希集合中,则继续遍历下一个节点;
如果当前节点在哈希集合中,则后面的节点都在哈希集合中,即从当前节点开始的所有节点都在两个链表的相交部分,因此在链表 headB 中遍历到的第一个在哈希集合中的节点就是两个链表相交的节点,返回该节点。
如果链表 headB 中的所有节点都不在哈希集合中,则两个链表不相交,返回 null。
复杂度分析
时间复杂度:O(m+n),其中 mm 和 nn 是分别是链表 headA 和 headB 的长度。需要遍历两个链表各一次。
空间复杂度:O(m),其中 mm 是链表 headA 的长度。需要使用哈希集合存储链表 headA 中的全部节点。
方法二:双指针
使用双指针的方法,可以将空间复杂度降至 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。
复杂度分析
时间复杂度:O(m+n)O(m+n),其中 mm 和 nn 是分别是链表 \textit{headA}headA 和 \textit{headB}headB 的长度。两个指针同时遍历两个链表,每个指针遍历两个链表各一次。
空间复杂度:O(1)O(1)。
5 我的答案
方法一:哈希集合
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
Set<ListNode> visited = new HashSet<ListNode>();
ListNode temp = headA;
while (temp != null) {
visited.add(temp);
temp = temp.next;
}
temp = headB;
while (temp != null) {
if (visited.contains(temp)) {
return temp;
}
temp = temp.next;
}
return null;
}
}
方法二:双指针
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;
}
}
边栏推荐
- Stream stream
- selenium3自动化测试(这一篇就够了)——自学篇
- MySQL query field matches the record of a rule
- [deserialization vulnerability-02] principle test and magic method summary of PHP deserialization vulnerability
- HDU 3351:Seinfeld
- Hcip OSPF interface network type experiment day 4
- JMeter interface test steps - Installation Tutorial - script recording - concurrent test
- Robot framework official tutorial (I) getting started
- 【反序列化漏洞-02】PHP反序列化漏洞原理测试及魔术方法总结
- About [software testing - interview skills and precautions for automated testing] - talk freely
猜你喜欢

使用Prometheus+Grafana实时监控服务器性能

Altium one key automatic BOM

Introduction to kubernetes Basics

Robot framework official tutorial (I) getting started

Jmeter-If控制器

Lanqiao cup provincial training camp - stack and recursion

RetinaNet:Focal Loss for Dense Object Detection

Docker installs 3 master and 3 slave redis clusters

【反序列化漏洞-01】序列化与反序列化简介

Use Modelsim to independently simulate Altera and Xilinx IP cores
随机推荐
关于【软件测试-自动化测试之面试技巧和注意事项】——侃侃而谈
[golang] golang implements MD5 encryption function
Text message verification of web crawler
Stm32+esp8266+mqtt protocol connects Alibaba cloud Internet of things platform
Selenium automated test (this one is enough) - self study
What is cloud native? Why is cloud native technology so popular?
SSH跨平台终端工具tabby推荐
Simply understand MODBUS function code and partition
Only "a little bit", why do developers look up to you?
STM32+ESP8266+MQTT协议连接阿里云物联网平台
使用Prometheus+Grafana实时监控服务器性能
【10】团队协作和跨团队协作
Robot Framework官方教程(一)入门
[golang] golang implements sha256 encryption function
16 tips for system administrators to use iptables
RRPN:Arbitrary-Oriented Scene Text Detection via Rotation Proposals
如何给自己的网站接入在线客服系统代码
What is the difference between strong reference, soft reference, weak reference and virtual reference?
Simply use MySQL index
In BS4.String and Difference between text