当前位置:网站首页>[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 ;
边栏推荐
- Some tips for programmers to deal with stress
- ava. Lang.noclassdeffounderror: org/apache/velocity/context/context solution
- Web3生态去中心化金融平台——Sealem Finance
- Docking of express bird system
- Hooks' design philosophy
- There is a problem with numpy after CONDA installs pytoch
- Application of object storage S3 in distributed file system
- MultipartFile和File互转工具类
- Leetcode divide and conquer method
- OCR文字识别经典论文详解
猜你喜欢

Yanrong looks at how to realize the optimal storage solution of data Lake in a hybrid cloud environment

How to use user-defined annotation for parameter verification

Garbled code when the command parameter contains% in VisualStudio debugging

Project_ Visual analysis of epidemic data based on Web Crawler

项目_基于网络爬虫的疫情数据可视化分析

2.1 ros+px4 simulation - Fixed Point flight control

1.5、PX4载具选择

How to download web photos

SAS因子分析(proc factor过程和因子旋转以及回归法求因子得分函数)

threejs:如何获得几何体的boundingBox?
随机推荐
MATLAB数字运算函数笔记
SAS cluster analysis (system cluster, dynamic cluster fastclus, variable cluster varclus)
云呐|PDA无线固定资产盘点管理系统
ROS parameter server
2.1、ROS+PX4仿真---定点飞行控制
hooks的设计哲学
Bad RequestThis combination of host and port requires TLS.
复利的保险理财产品怎么样?可以买吗?
项目_基于网络爬虫的疫情数据可视化分析
Hooks' design philosophy
Configurable custom implementation 1 Implementation interface, 2 Custom configuration 3 Default configuration
SSH Remote Login configuration sshd_ Config file details
Detectron2 trains its own dataset and converts it to coco format
1.2. Ros+px4 preliminary basic knowledge
Implementing MySQL fuzzy search with node and express
Leetcode linked list queue stack problem
Be careful, these hidden "bugs" of "array" to "collection"“
"It looks like robbing tickets but actually robbing money". Don't be fooled by fancy ticket robbing products again and again
A/B机器正常连接后, B机器突然重启, 问A此时处于TCP的 什么状态?如何消除服务器程序中的这个状态?
CSRF攻击