当前位置:网站首页>力扣160题,相交链表
力扣160题,相交链表
2022-08-01 12:33:00 【瀛台夜雪】
力扣160题,相交链表
题目描述
给你两个单链表的头节点 headA 和 headB ,请你找出并返回两个单链表相交的起始节点。如果两个链表不存在相交节点,返回 null 。
图示两个链表在节点 c1 开始相交:
题目数据 保证 整个链式结构中不存在环。
注意,函数返回结果后,链表必须 保持其原始结构 。
自定义评测:
评测系统 的输入如下(你设计的程序 不适用 此输入):
intersectVal - 相交的起始节点的值。如果不存在相交节点,这一值为 0
listA - 第一个链表
listB - 第二个链表
skipA - 在 listA 中(从头节点开始)跳到交叉节点的节点数
skipB - 在 listB 中(从头节点开始)跳到交叉节点的节点数
评测系统将根据这些输入创建链式数据结构,并将两个头节点 headA 和 headB 传递给你的程序。如果程序能够正确返回相交节点,那么你的解决方案将被 视作正确答案 。
输入输出样例

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

输入:intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
输出:Intersected at '2'
解释:相交节点的值为 2 (注意,如果两个链表相交则不能为 0)。
从各自的表头开始算起,链表 A 为 [1,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 。
解法一:使用hash进行处理
//算法一:使用hash表进行查找相交的链表
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB)
{
if(!headA||!headB)
{
return nullptr;
}
//建立hash表保存headA所有的结点
unordered_set<ListNode *>uset;
ListNode *tempNodeA=headA;
while(tempNodeA)
{
uset.insert(tempNodeA);
tempNodeA=tempNodeA->next;
}
//建立临时指针指向headB的结点
ListNode *tempNodeB=headB;
while(tempNodeB)
{
if(uset.count(tempNodeB))
{
return tempNodeB;
}
tempNodeB=tempNodeB->next;
}
return nullptr;
}
解法二,使用双指针的方法,时间复杂度为O(m+n),空间为O(1)
//解法二,使用双指针的方法,两个指针指向两个链表,谁先遍历完便指向另一个链表,直到两个指针都为空或则两个指针找到相同的值
ListNode *getIntersectionNode2(ListNode *headA, ListNode *headB)
{
if(!headA||!headB)
{
return nullptr;
}
//建立临时结点指向headA,headB
ListNode *tempNodeA,*tempNodeB;
tempNodeA=headA;
tempNodeB=headB;
while(tempNodeA!=tempNodeB)
{
if(tempNodeA)
{
tempNodeA=tempNodeA->next;
}
else{
tempNodeA=headB;
}
if(tempNodeB)
{
tempNodeB=tempNodeB->next;
}
else{
tempNodeB=headA;
}
}
return tempNodeA;
}
边栏推荐
- SQL函数 STR
- 判断JS数据类型的四种方法
- bat countdown code
- [Open class preview]: Research and application of super-resolution technology in the field of video image quality enhancement
- Transfer learning to freeze the network:
- 初级必备:单例模式的7个问题
- 2022 Go ecosystem rpc framework Benchmark
- Towhee 每周模型
- [Community Star Selection] Issue 24 August Update Plan | Keep writing, refuse to lie down!More original incentive packages, as well as Huawei WATCH FIT watches!
- 重磅消息 | Authing 实现与西门子低代码平台的集成
猜你喜欢
随机推荐
Grafana 9.0 released, Prometheus and Loki query builders, new navigation, heatmap panels and more!
leetcode/submatrix element sum
Istio Meetup China: Full Stack Service Mesh - Aeraki Helps You Manage Any Layer 7 Traffic in an Istio Service Mesh
SCHEMA解惑
How to integrate 3rd party service center registration into Istio?
动态库、静态库浅析
AI目标分割能力,无需绿幕即可实现快速视频抠图
易周金融分析 | 银行ATM机智能化改造提速;互联网贷款新规带来挑战
[CLion] CLion always prompts "This file does not belong to any project target xxx" solution
ddl and dml in sql (the difference between database table and view)
R语言ggplot2可视化:使用ggpubr包的ggscatter函数可视化散点图、使用xscale函数指定X轴坐标轴度量调整方式、设置x轴坐标为scientific使用科学计数法显示坐标值
Pytest e-commerce project combat (below)
表连接详解
js中常用追加元素的几种方法:append,appendTo,after,before,insertAfter,insertBefore,appendChild
Simulation implementation of new of Js handwritten function
批量任务导入到数据库中
判断JS数据类型的四种方法
How to get the address of WeChat video account (link address of WeChat public account)
(ES6 and above and TS) Map object to array
Istio Meetup China:全栈服务网格 - Aeraki 助你在 Istio 服务网格中管理任何七层流量









