当前位置:网站首页>Linked list: the first coincident node of two linked lists
Linked list: the first coincident node of two linked lists
2022-06-13 02:44:00 【Zeng Qiang】
subject
https://leetcode-cn.com/problems/3u1WK4/
Find the first coincidence node of two linked lists
Above picture , c1 That is, two linked lists A、B The first coincident node of .
Their thinking
According to the meaning , If two linked lists are the same length , Then define the double pointer , Respectively point to the head node of the linked list , And move right at the same time , If they overlap , Then the values of the current two pointers are equal , Just go back to the node .
If the two linked lists are different , Then we need to find the length of the two linked lists first , The pointer of the linked list with longer length moves forward first delta Step , Restore the length of the linked list to the same state , Repeat the previous step , You can get the answer .
So the steps of this question are divided into two steps :
- Find the length of two linked lists
- Define double pointer . First move the long linked list delta Step . Move both pointers at the same time , Until the nodes pointed to by the two pointers are equal .
Code
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
// Count the length of two linked lists
int aListLength = countList(headA);
int bListLength = countList(headB);
// Move the long list delta Step (diff = longListLength - shortListLength)
int delta = Math.abs(aListLength - bListLength);
ListNode longerList = null;
ListNode shortList = null;
if(aListLength > bListLength){
longerList = headA;
shortList = headB;
} else {
longerList = headB;
shortList = headA;
}
for(int i = 0; i < delta; i++) {
longerList = longerList.next;
}
// Move the pointer on the two linked lists at the same time , Return to the node you first met .
while (longerList != shortList) {
longerList = longerList.next;
shortList = shortList.next;
}
return shortList;
}
private int countList(ListNode head) {
int count = 0;
while(head != null) {
count++;
head = head.next;
}
return count;
}
}
summary
This topic examines the intersection nodes of two linked lists . The original problem can be divided into two sub problems , Find the length of the linked list and find the nodes equal to the linked list . This problem can be solved by using the speed pointer .
边栏推荐
- [data and Analysis Visualization] D3 introductory tutorial 2- building shapes in D3
- Opencv 15 face recognition and eye recognition
- Opencv 17 face recognition
- [reading papers] visual convolution zfnet
- Queuing theory, game theory, analytic hierarchy process
- How to destroy a fragment- How to destroy Fragment?
- 01 初识微信小程序
- Hstack, vstack and dstack in numpy
- 遍历数组,删除某元素,直到删除为止
- Resource arrangement
猜你喜欢
[reading papers] comparison of deeplobv1-v3 series, brief review
Introduction and download of common data sets for in-depth learning (with network disk link)
在IDEA使用C3P0连接池连接SQL数据库后却不能显示数据库内容
数仓笔记|针对客户维度建模需要关注的5个因素
Automatic differential reference
JS deconstruction assignment
[reading papers] transformer miscellaneous notes, especially miscellaneous
[data and Analysis Visualization] data operation in D3 tutorial 3-d3
Summary of innovative ideas of transformer model in CV
Bi modal progressive mask attention for fine graded recognition
随机推荐
PCR validation of basic biological experiments in [life sciences]
[data analysis and visualization] key points of data drawing 9- color selection
[deep learning] fast Reid tutorial
[thoughts in the essay] mourn for development technology expert Mao Xingyun
Opencv 17 face recognition
[reading papers] dcgan, the combination of generating countermeasure network and deep convolution
如何挑选基金产品?什么样的基金是好基金?
[data analysis and visualization] key points of data drawing 12- importance of chart notes
Node uses post to request req Pit with empty body
Detailed explanation of data processing in machine learning (I) -- missing value processing (complete code attached)
Detailed installation tutorial of MATLAB r2019 B-mode ultrasound (complete installation files are attached)
js多种判断写法
[reading papers] transformer miscellaneous notes, especially miscellaneous
冲刺强基计划数学物理专题一
Stm32f4 DMA Da sine wave generator keil5 Hal library cubemx
Opencvshare4 and vs2019 configuration
Vant框架中关于IndexBar索引栏的CDN单页面引用,无法正常展示
Superficial understanding of conditional random fields
Hstack, vstack and dstack in numpy
03 认识第一个view组件