当前位置:网站首页>Linked list intersection (linked list)
Linked list intersection (linked list)
2022-04-23 10:15:00 【Popuessing's Jersey】
subject :
Given two ( A one-way ) Linked list , Determine whether they intersect and return to the intersection point . Note that the definition of intersection is based on the node reference , Not based on the value of the node . let me put it another way , If the second part of a linked list k A node and the second... Of another linked list j Two nodes are the same node ( References are exactly the same ), Then the two linked lists intersect .
Ideas : Align the tail of the linked list , Look for the same part from a certain position to the tail
public class Lianbiaoxiangjiao {
public ListNode getIntersectionNode(ListNode headA,ListNode headB){
ListNode curA = headA;
ListNode curB = headB;
int lenA = 0,lenB = 0;
while (curA != null){
// Find the linked list A The length of
lenA++;
curA = curA.next;
}
while (curB !=null){
// Find the linked list B The length of
lenB++;
curB = curB.next;
}
// Restore A,B Object pointer
curA = headA;
curB = headB;
// Give Way curA For the head of the longest linked list ,lenA Is its length
if (lenB > lenA){
int temp = lenA;
lenA = lenB;
lenB = temp;
ListNode tempNode = curA;
curA = curB;
curB = tempNode;
}
// Length difference
int gap = lenA - lenB;
// Give Way curA and curB Last position alignment
while (gap>0){
curA = curA.next;
gap--;
}
// Traverse curA and curB, If you encounter the same, you will directly return
while (curA !=null){
if (curA == curB){
return curA;
}
curA = curA.next;
curB = curB.next;
}
// If not found, return null
return null;
}
}
版权声明
本文为[Popuessing's Jersey]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231011140930.html
边栏推荐
- 域名和IP地址的联系
- Understand the new economic model of platofarm and its ecological progress
- Examination questions and answers of the third batch (main person in charge) of Guangdong safety officer a certificate in 2022
- Configuration of LNMP
- 转:毛姆:阅读是一座随身携带的避难所
- JUC concurrent programming 09 -- source code analysis of condition implementation
- Windows installs redis and sets the redis service to start automatically
- JVM——》常用命令
- Go语言实践模式 - 函数选项模式(Functional Options Pattern)
- Common DBA SQL statements (4) - Top SQL
猜你喜欢
Sim Api User Guide(6)
2022年制冷与空调设备运行操作考试练习题及模拟考试
实践六 Windows操作系统安全攻防
《Redis设计与实现》
JUC concurrent programming 06 -- in-depth analysis of AQS source code of queue synchronizer
解决方案架构师的小锦囊 - 架构图的 5 种类型
Computer network security experiment II DNS protocol vulnerability utilization experiment
Juc并发编程09——Condition实现源码分析
Read LSTM (long short term memory)
深度选择器
随机推荐
2022 mobile crane driver test question bank simulation test platform operation
Solve the problem of installing VMware after uninstalling
JVM——》常用参数
Pyqt5 and communication
Reading integrity monitoring techniques for vision navigation systems - 3 background
What are the system events of Jerry's [chapter]
MapReduce core and foundation demo
Function realization of printing page
Juc并发编程07——公平锁真的公平吗(源码剖析)
CSP认证 202203-2 出行计划(多种解法)
第一章 Oracle Database In-Memory 相关概念(续)(IM-1.2)
Ansible cloud computing automation
Realize data value through streaming data integration (3) - real-time continuous data collection
Solution architect's small bag - 5 types of architecture diagrams
Go语言实践模式 - 函数选项模式(Functional Options Pattern)
Realize data value through streaming data integration (1)
2022年上海市安全员C证考试题库及答案
206、反转链表(链表)
Sim Api User Guide(8)
Jerry sometimes finds that the memory has been tampered with, but there is no exception. How should he find it? [chapter]