当前位置:网站首页>[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
边栏推荐
- 管道和xargs
- Selenium displays webdriverwait
- Thinking after learning to type
- Unit function expansion
- Problem brushing plan -- dynamic programming (IV)
- Pyuninstaller package exe cannot find the source code when running, function error oserror: could not get source code
- MySQL basic collection
- Write table of MySQL Foundation (create table)
- C#,打印漂亮的贝尔三角形(Bell Triangle)的源程序
- MySQL进阶之数据的增删改查(DML)
猜你喜欢
[leetcode refers to the maximum profit of offer 63. Stock (medium)]
Arm architecture assembly instructions, registers and some problems
Recommended usage scenarios and production tools for common 60 types of charts
[※ leetcode refers to offer 32 - II. Print binary tree II from top to bottom (simple)]
Common problems in deploying projects with laravel and composer for PHP
FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ‘:app:stripDe
ROS learning notes - tutorial on the use of ROS
Minecraft 1.12.2模组开发(四十三) 自定义盾牌(Shield)
MySQL进阶之数据的增删改查(DML)
Question brushing plan - depth first search DFS (I)
随机推荐
Pytorch preserves different forms of pre training models
opencv应用——以图拼图
Deno 1.13.2 发布
电脑越用越慢怎么办?文件误删除恢复方法
[SDU chart team - core] enumeration of SVG attribute class design
Keywords static, extern + global and local variables
C#,打印漂亮的贝尔三角形(Bell Triangle)的源程序
Valueerror: invalid literal for int() with base 10 conversion error related to data type
MySQL进阶之表的增删改查
又一款数据分析神器:Polars 真的很强大
Question brushing plan -- backtracking method (I)
GSI-ECM工程建设管理数字化平台
Win 11K in 100 days, super complete learning guide for job transfer test
Explore ASP Net core read request The correct way of body
Addition, deletion, modification and query of MySQL advanced table
Centos7 builds MySQL master-slave replication from scratch (avoid stepping on the pit)
On the three paradigms of database design
Introduction to tensorrt
引入结构化并发,Swift 5.5 发布!
[leetcode refers to offer 25. Merge two sorted linked lists (simple)]