当前位置:网站首页>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
边栏推荐
- ansible 云计算 自动化
- The central control learning infrared remote control module supports network and serial port control
- Realizing data value through streaming data integration (4) - streaming data pipeline
- Ansible cloud computing automation command line compact version
- 2022 mobile crane driver test question bank simulation test platform operation
- Zhengda international explains what the Dow Jones industrial index is?
- Formattime timestamp format conversion
- Chapter II in memory architecture (im-2.2)
- 解决VMware卸载后再安装出现的问题
- 101. Symmetric Tree
猜你喜欢

Computer network security experiment II DNS protocol vulnerability utilization experiment

一文看懂 LSTM(Long Short-Term Memory)

2022 mobile crane driver test question bank simulation test platform operation

IDEA——》每次启动都会Indexing或 scanning files to index

JUC concurrent programming 06 -- in-depth analysis of AQS source code of queue synchronizer

"Gu Yu series" airdrop

shell脚本免交互

101. Symmetric Tree

Configuration of LNMP

Initial exploration of NVIDIA's latest 3D reconstruction technology instant NGP
随机推荐
ansible 云计算 自动化 命令行精简版
第二章 Oracle Database In-Memory 体系结构(上) (IM-2.1)
Using idea to develop Spark Program
Go语言实践模式 - 函数选项模式(Functional Options Pattern)
Realize data value through streaming data integration (3) - real-time continuous data collection
第二章 In-Memory 体系结构 (IM-2.2)
Can Jerry's AES 256bit [chapter]
2022茶艺师(初级)考试试题模拟考试平台操作
Jerry's more accurate determination of abnormal address [chapter]
Chapter 3 enable and adjust the size of IM column storage (im-3.1)
"Gu Yu series" airdrop
通过流式数据集成实现数据价值(2)
LeetCode 1249. Minimum remove to make valid parents - FB high frequency question 1
Depth selector
通过流式数据集成实现数据价值(5)- 流分析
Juc并发编程06——深入剖析队列同步器AQS源码
Detailed explanation of MapReduce calculation process
一文读懂PlatoFarm新经济模型以及生态进展
JUC concurrent programming 09 -- source code analysis of condition implementation
Formattime timestamp format conversion