当前位置:网站首页>[leetcode] sword finger offer 52 The first common node of two linked lists
[leetcode] sword finger offer 52 The first common node of two linked lists
2022-07-21 01:54:00 【Spring-_- Bear】
Enter two linked lists , Find their first common node .
For example, the following two linked lists are at the node c1 Start meeting .
Example 1:
Input :intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
Output :Reference of the node with value = 8
Enter an explanation : The value of the intersection node is 8 ( Be careful , Cannot be if two lists intersect 0). Starting from the respective heads , Linked list A by [4,1,8,4,5], Linked list B by [5,0,1,8,4,5]. stay A in , There is... Before the intersection node 2 Nodes ; stay B in , There is... Before the intersection node 3 Nodes .
Example 2:
Input :intersectVal = 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
Output :Reference of the node with value = 2
Enter an explanation : The value of the intersection node is 2 ( Be careful , Cannot be if two lists intersect 0). Starting from the respective heads , Linked list A by [0,9,1,2,4], Linked list B by [3,2,4]. stay A in , There is... Before the intersection node 3 Nodes ; stay B in , There is... Before the intersection node 1 Nodes .
Example 3:
Input :intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
Output :null
Enter an explanation : Starting from the respective heads , Linked list A by [2,6,4], Linked list B by [1,5]. Because these two linked lists don't intersect , the > With intersectVal It has to be for 0, and skipA and skipB It could be any value .
explain : The two lists don't intersect , Therefore return null.
Be careful :
If there is no intersection between two linked lists , return null.
After returning the result , The two lists still have to keep their original structure .
It can be assumed that there is no cycle in the whole linked list structure .
The procedure is as satisfying as possible O(n) Time complexity , And only O(1) Memory .
Answer key :
/** * The finger of the sword Offer 52. The first common node of two linked lists */
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
/* * set up headA The number of nodes is a,headB The number of nodes is b, The number of common nodes of the two linked lists is c, The public node is node * headA To node Former co owner a-c Nodes ,headB To node Former co owner b−c Nodes , Two hands A and B Point to respectively headA and headB * A End of traversal headA Continue traversal after headB,B End of traversal headB Continue traversal after headA * When A and B When we met , here A The number of steps taken is a+(b-c),B The number of steps taken is b+(a-c) * * I walked through my world , Go through your world again * You walk through your world , Go through my world again * Eventually we will meet , Maybe on the way ( Public nodes ), Maybe at the end (null) */
ListNode A = headA;
ListNode B = headB;
while (A != B) {
A = A != null ? A.next : headB;
B = B != null ? B.next : headA;
}
return A;
}
source : Power button (LeetCode)
link :https://leetcode.cn/problems/liang-ge-lian-biao-de-di-yi-ge-gong-gong-jie-dian-lcof
边栏推荐
- 技术干货 | 基于 MindSpore 实现 CosineSimilarity
- Review of interview questions 2
- 0.0. Pytorch model building method
- 机器学习基础篇(2)之基础知识和绘制图形
- C——字符串
- Ampere Altra Max 提供可持续的高分辨率 H.265 编码
- [server data recovery] data recovery of a brand ProLiant server raid paralysis database file damage
- 移动端测试必备技能: adb命令和抓包
- Configure dual database
- 力扣343-整数分割——动态规划
猜你喜欢
都2022年了,你不会还不知道什么是自动化测试吧....
Understanding and applying continuous integration Tekton
深度学习1-感知器
从镜像仓库工具、镜像下载加速工具、安全扫描工具理解镜像存储和镜像安全
Go daily Gore
It's 2022, and you don't know what automated testing is
[R language text mining]: emotion analysis and word cloud mapping
如何选择数据应用开发语言和环境
20元一支的洗面奶,7天卖了上万,他们是如何做到的?
开发者分享|『啃书吧:深度学习与MindSpore实践』第二期 回归分析
随机推荐
About: Customizing templates in office 2021
[LeetCode]剑指 Offer 53 - I. 在排序数组中查找数字 I
【琐琐碎碎小知识】 关于部分Unity编辑器在创建瓦片地图时缺乏Tiles选项
A good resume can always brighten people's eyes during the interview of the testing post
MATLAB:将figure图打印成pdf格式
原生高性能抓包工具Proxyman,送给爱学习的你
Use OpenCV to adjust the brightness and contrast of the image
Fundamentals of machine learning (2): basic knowledge and drawing graphics
AT32 MCU F415 OTG新功能使用
[R language text mining]: emotion analysis and word cloud mapping
DOM系列之节点操作
计算任意根号n的值
过滤器和拦截器区别
邮件推送平台-外贸推广
视频25-7章2节VGG 26-NiN 27-GooLeNet
技术干货 | MindSpore 自研高阶优化器源码分析和实践应用
LeetCode__ 301 weekly games 6112. Minimum total time required to fill the cup___ Greedy two methods
LeetCode. 302 weekly games___ 02_ 6164. Maximum sum of digits and equal pairs___ Hash + enhanced preprocessing + custom priority queue
NETFLOW 与 SNMP两种不同的网络监控方法
10 port scanning tools for advanced scanning by network administrators