当前位置:网站首页>[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 ;
边栏推荐
- MATLAB数字运算函数笔记
- About mobx
- Leetcode 1116 print zero even odd (concurrent multithreading countdownlatch lock condition)
- What are programmers in big factories looking at? It took me two years to sort it out, and I will look at it and cherish it!
- 2.2、ROS+PX4仿真多点巡航飞行----正方形
- 部分 力扣 LeetCode 中的SQL刷题整理
- Leetcode divide and conquer method
- 2021-3-1MATLAB写cnn的mnist数据库训练
- Threejs: pit encountered in drawing Bessel curve with two-point coordinates
- Sealem finance builds Web3 decentralized financial platform infrastructure
猜你喜欢

Projet Visualisation et analyse des données sur les épidémies basées sur le Web crawler

Hao expresses his opinions: what small good habits have you adhered to?

Sealem finance builds Web3 decentralized financial platform infrastructure

Linux安装mysql数据库详解

How to download web photos

How to use user-defined annotation for parameter verification

Daily problem essay | 21.11.29: use resttemplate to call external put request, and prompt '400 bad request'

云呐|PDA无线固定资产盘点管理系统

Multi interest recall model practice | acquisition technology

2.2、ROS+PX4仿真多点巡航飞行----正方形
随机推荐
中间件_Redis_05_Redis的持久化
1.7、PX4遥控器校准
Docking of express bird system
What are programmers in big factories looking at? It took me two years to sort it out, and I will look at it and cherish it!
Middleware_ Redis_ 06_ Redis transactions
LeetCode 1029 Two City Scheduling (dp)
Hooks' design philosophy
I was so excited about the college entrance examination in 2022
From "0" to "tens of millions" concurrency, 14 technological innovations of Alibaba distributed architecture
[recommended by Zhihu knowledge master] castle in UAV - focusing on the application of UAV in different technical fields
1.3 introduction to ROS UAV
关于概率统计中的排列组合
QGC ground station tutorial
[geometric vision] 4.2 piecewise linear transformation
[ongoing update...] 2021 National Electronic Design Competition for college students (III) interpretation of the anonymous four axis space developer flight control system design
数字ic设计自学ing
1.3 ROS 无人机简介
SAS cluster analysis (system cluster, dynamic cluster fastclus, variable cluster varclus)
Using completabilefuture
Px4 installation tutorial (VI) vertical fixed wing (tilting)