当前位置:网站首页>2022.08.01_Daily Question
2022.08.01_Daily Question
2022-08-05 11:54:00 【No. い】
剑指 Offer 52. 两个链表的第一个公共节点
题目描述
输入两个链表,找出它们的第一个公共节点.
如下面的两个链表:

在节点 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) 内存.
- 本题与主站 160 题相同:https://leetcode-cn.com/problems/intersection-of-two-linked-lists/
coding
1. 借用 List 存储,然后遍历另一条链表,By judging whether the node already exists,判断是否相交
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */
class Solution {
ListNode getIntersectionNode(ListNode headA, ListNode headB) {
List<ListNode> list = new ArrayList<>();
ListNode cur1 = headA;
while (cur1 != null) {
list.add(cur1);
cur1 = cur1.next;
}
ListNode cur2 = headB;
while (cur2 != null) {
if (list.contains(cur2)) {
return cur2;
}
cur2 = cur2.next;
}
return null;
}
}
2. 如果两链表相交,Then the end part must be the same,The number of nodes is also the same,First filter the nodes that are necessarily impossible to intersect at the beginning
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */
class Solution {
ListNode getIntersectionNode(ListNode headA, ListNode headB) {
// Record the length of the two linked lists
int len1 = 0, len2 = 0;
ListNode cur1 = headA;
while (cur1 != null) {
len1 ++;
cur1 = cur1.next;
}
ListNode cur2 = headB;
while (cur2 != null) {
len2 ++;
cur2 = cur2.next;
}
ListNode temp1 = len1 > len2 ? headA : headB;
ListNode temp2 = len1 > len2 ? headB : headA;
int cnt = Math.abs(len1 - len2);
// Make the linked list longertemp1Filter out the excess length(The intersection necessarily leads to the same number of nodes in the common part)
while (cnt-- > 0) {
temp1 = temp1.next;
}
// When the number of remaining nodes is the same,Traverse the nodes at the end to determine whether there is an intersection(temp1 == temp2)
while (temp1 != null) {
if (temp1 == temp2) {
return temp1;
}
temp1 = temp1.next;
temp2 = temp2.next;
}
return null;
}
}
边栏推荐
- PMP每日一练 | 考试不迷路-8.5(包含敏捷+多选)
- How about Ping An Mengwa Card Insurance?Let parents read a few ways to identify products
- 澳洲站:电吹风AS/NZS 60335.2.23: 2017 安全标准测试
- 623. 在二叉树中增加一行 : 简单二叉树遍历运用题
- Reflect中的方法
- Gray value and thermal imaging understanding
- 前沿技术数字孪生如何应用在智慧城市上?
- 2022 CCF国际AIOps挑战赛决赛暨AIOps研讨会报名已开启
- D-Desthiobiotin-PEG4-Maleimide主要物理性质特点 !
- Go 语言快速入门指南: 基本类型
猜你喜欢

详细剖析 Redis 三种集群策略

深度学习(四)分析问题与调参 理论部分

PMP每日一练 | 考试不迷路-8.5(包含敏捷+多选)

STM32H743IIT6学习笔记03——使用第三方组件FreeRTOS

【HMS core】【FAQ】Health Kit、Ads kit、push Kit典型问题合集5

官方发布·2022南京智博会定于10月份在新庄国展召开

2021 RoboCom 世界机器人开发者大赛-高职组(决赛)

Cesium.js 三维土壤地质剖面分割挖掘

No developers, received a job to develop an IoT system, do you want to do it?

【着色器实现Flicker“DJ”闪烁效果_Shader效果第十五篇】
随机推荐
灰度值与热成像理解
深度学习(四)分析问题与调参 理论部分
Go 语言 strings 库常用方法
莅临GOPS大会龙智展位,获取Forrester最新报告:《Forrester Wave:2021年第四季度企业服务管理报告》
Go 语言快速入门指南: 基本类型
手把手教你定位线上MySQL慢查询问题,包教包会
Flink Yarn Per Job - RM启动SlotManager
790. 数的三次方根
力扣330 按要求补齐数组(贪心)
Flink Yarn Per Job - JobManger 申请 Slot
2022.08.02_每日一题
163_Tricks_Power BI one-click batch creation of custom field parameters
623. 在二叉树中增加一行 : 简单二叉树遍历运用题
Byte Qiu Zhao confused me on both sides, and asked me under what circumstances would the SYN message be discarded?
Go编译原理系列9(函数内联)
花的含义
Qt::qcustomplot 和 qchart数据填充相关
软件设计七大原则之开闭原则(Open-Closed Principle, OCP)
C语言经典例题-求一串数中的最大数
2022杭电多校联赛第六场 题解