当前位置:网站首页>[leetcode refers to offer 52. The first common node of two linked lists (simple)]
[leetcode refers to offer 52. The first common node of two linked lists (simple)]
2022-04-23 21:21:00 【Minaldo7】
subject :
Enter two linked lists , Find their first common node .
Like the two linked lists below :
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 , therefore 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 .
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/liang-ge-lian-biao-de-di-yi-ge-gong-gong-jie-dian-lcof
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
The problem solving process :
source LeetCode user : gem
Set intersection list length c, Linked list 1 Except that the length of the intersection is a, Linked list 2 Except that the length of the intersection is b, Yes
a + c + b = b + c + a
If there is no intersection , be a + b = b + a
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if(headA==null || headB==null)
return null;
ListNode n1 = headA, n2 = headB;
while(n1!=n2){
n1 = n1==null ? headB:n1.next;
n2 = n2==null ? headA:n2.next;
}
return n1;
}
}
Execution results :
版权声明
本文为[Minaldo7]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/111/202204210544479139.html
边栏推荐
- [leetcode refers to the substructure of offer 26. Tree (medium)]
- pikachuxss如何获取cookie靶场,返回首页总是失败
- Reference of custom message in ROS function pack failed
- MySQL数据库常识之储存引擎
- Google 尝试在 Chrome 中使用 Rust
- Is qiniu school useful and is the recommended securities account safe
- Opencv reports an error. Expected PTR < CV:: UMAT > for argument '% s'‘
- Chrome 94 introduces the controversial idle detection API, which apple and Mozilla oppose
- Sequential state
猜你喜欢
Fastdfs mind map
[leetcode refers to the maximum profit of offer 63. Stock (medium)]
Opencv application -- jigsaw puzzle
thinkphp5+数据大屏展示效果
Google 尝试在 Chrome 中使用 Rust
[leetcode refers to offer 42. Maximum sum of continuous subarrays (simple)]
Chrome 94 引入具有争议的 Idle Detection API,苹果和Mozilla反对
Zhongchuang storage | how to choose a useful distributed storage cloud disk
2.整理华子面经--2
Is rust more suitable for less experienced programmers?
随机推荐
Pytorch: runtimeerror: an attempt has been made to start a new process Error reporting (resolved)
thinkphp5+数据大屏展示效果
Sharpness difference (SD) calculation method of image reconstruction and generation domain index
Ubuntu 20 installing centernet
Question brushing plan - depth first search (II)
Two Stage Detection
Google 尝试在 Chrome 中使用 Rust
Google tries to use rust in Chrome
DeNO 1.13.2 release
Problem brushing plan -- dynamic programming (IV)
Question brushing plan - depth first search DFS (I)
Thinking after learning to type
Pytorch preserves different forms of pre training models
Chrome 94 引入具有争议的 Idle Detection API,苹果和Mozilla反对
How to play the guiding role of testing strategy
如何发挥测试策略的指导性作用
Deno 1.13.2 发布
Detailed explanation of basic assembly instructions of x86 architecture
Go limit depth traversal of files in directory