当前位置:网站首页>[leetcode] intersecting linked list
[leetcode] intersecting linked list
2022-06-11 01:45:00 【xiaoshijiu333】
#LeetCode A daily topic 【 Special topic of linked list 】
Intersecting list
https://leetcode-cn.com/problems/intersection-of-two-linked-lists/analysis
The ring linked list has been implemented , How to find the position of the ring . This problem can be regarded as a derivative of the circular linked list , Find the location where the linked list intersects :- Construct a ring ,headA A round of traversal , End to end connection ring
- At this point is a circular linked list to find the location of the ring : Use double pointer , After a quick and slow encounter , Reset the slow pointer to the starting point and start again at the same time , When they meet, they are in the ring position : See :
https://blog.csdn.net/shijiujiu33/article/details/122544237 - It should be noted that , The title requires to keep the original linked list structure unchanged , After finding the intersection , Ring break required
Realization
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
// Find the location where two linked lists meet
// Similar to circular linked list , Find the location of the ring
// First traversal , take A Linked lists form rings ; Then there is the problem of a circular linked list
// A Two steps at a time ,B One step at a time ; After meeting , Set one of them as the head node , Walking at the same time , The second meeting is the position of entering the ring ( Intersection location )
if (headA == null || headB == null) {
return null;
}
ListNode temp = headA;
while (temp.next != null) {
temp = temp.next;
}
temp.next = headA;
ListNode left = headB, right = headB;
while (right != null && right.next != null) {
right = right.next.next;
left = left.next;
if (left == right) {
break;
}
}
left = headB;
while (right != null && right.next != null) {
if (left == right) {
// Broken ring ( Keep the original structure unchanged )
temp.next = null;
return left;
}
left = left.next;
right = right.next;
}
// Broken ring ( Keep the original structure unchanged )
temp.next = null;
return null;
}
LeetCode Time consuming :1ms
- Achieve two
Alternate traversal ,headA After traversing , from headB To traverse the ; meanwhile headB After traversing , from headA To traverse the , Then the position where they meet is determined as the intersection point :
It's also a math problem
public ListNode getIntersectionNode02(ListNode headA, ListNode headB) {
// Alternate traversal ,headA After traversing , from headB To traverse the ; meanwhile headB After traversing , from headA To traverse the , Then the position where they meet is determined as the intersection point
if (headA == null || headB == null) {
return null;
}
ListNode tempA = headA, tempB = headB;
// There is no intersection , Will be in null=null meet
while (tempA != tempB) {
tempA = tempA == null ? headB : tempA.next;
tempB = tempB == null ? headA : tempB.next;
}
return tempA;
}
LeetCode Time consuming :1ms
- summary
Be good at changing topics into familiar ones ;
The position of the intersection can be converted to the position of the finding ring ;
边栏推荐
- Leetcode 1814 count nice pairs in an array (recommended by map)
- ros缺包怎么办
- Clean up the broken artifacts data (.lastUpdated files) and reload the project. Problem resolution
- 1.2、ROS+PX4预备基础知识
- I was so excited about the college entrance examination in 2022
- 2021-07-18 ROS笔记-基础和通讯
- [interpretation of the paper] sort out the papers on the vision based autonomous landing platform of UAV
- Configurable custom implementation 1 Implementation interface, 2 Custom configuration 3 Default configuration
- 1.6 Px4 initialization calibration
- 项目_基于网络爬虫的疫情数据可视化分析
猜你喜欢

多兴趣召回模型实践|得物技术

SAS判别分析(Bayes准则和proc discrim过程)

中间件_Redis_06_Redis的事务

OCR文字识别经典论文详解

A tutorial on building a website from scratch with complete steps (7000 words and 102 screenshots for everyone to understand, with source code attached)

Garbled code when the command parameter contains% in VisualStudio debugging

Function of barcode fixed assets management system, barcode management of fixed assets
![[Li mu] how to read papers [intensive reading of papers]](/img/41/7e1ff1db2f7a848c8702c186c79fe5.jpg)
[Li mu] how to read papers [intensive reading of papers]

ROS参数服务器

Leetcode binary tree problem
随机推荐
Current limiting and download interface request number control
SAS因子分析(proc factor过程和因子旋转以及回归法求因子得分函数)
腾讯云数据库TDSQL-大咖论道 | 基础软件的过去、现在、未来
SQL question brushing and sorting in leetcode of partial deduction
There is a problem with numpy after CONDA installs pytoch
Garbled code when the command parameter contains% in VisualStudio debugging
1.7、PX4遥控器校准
ros缺包怎么办
[ongoing update...] 2021 National Electronic Design Competition for college students (III) interpretation of the anonymous four axis space developer flight control system design
Role of handlermethodargumentresolver + use case
部分 力扣 LeetCode 中的SQL刷题整理
1.5 Px4 vehicle selection
Record the packaging of the googlechrome browser plug-in
Web3 ecological decentralized financial platform sealem Finance
[geometric vision] 4.2 piecewise linear transformation
数字ic设计自学ing
立个flag--重构promise
Leetcode 698 partition to K equal sum subsets (DFS pruning)
ava. Lang.noclassdeffounderror: org/apache/velocity/context/context solution
SAS cluster analysis (system cluster, dynamic cluster fastclus, variable cluster varclus)