当前位置:网站首页>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 .
边栏推荐
- A wechat app for shopping
- Use of OpenCV 12 findcircuits and drawcircuits
- Matlab: obtain the figure edge contour and divide the figure n equally
- 04路由跳转并携带参数
- L1 regularization and its sparsity
- [data analysis and visualization] key points of data drawing 9- color selection
- Use of OpenCV 11 kmeans clustering
- 冲刺强基计划数学物理专题一
- Data processing in detailed machine learning (II) -- Feature Normalization
- How can intelligent safe power distribution devices reduce the occurrence of electrical fire accidents?
猜你喜欢

Prometheus node_exporter安装并注册为服务

Detailed explanation of data processing in machine learning (I) -- missing value processing (complete code attached)

Introduction to facial expression recognition system - Technical Paper Edition

专业的数据库管理软件:Valentina Studio Pro for Mac

在IDEA使用C3P0連接池連接SQL數據庫後卻不能顯示數據庫內容

Matlab: obtain the figure edge contour and divide the figure n equally
![[reading papers] deep learning face representation by joint identification verification, deep learning applied to optimization problems, deepid2](/img/a1/151d2afe6d7f0bd95fe93fc80f633e.jpg)
[reading papers] deep learning face representation by joint identification verification, deep learning applied to optimization problems, deepid2

專業的數據庫管理軟件:Valentina Studio Pro for Mac

Detailed explanation of handwritten numeral recognition based on support vector machine (Matlab GUI code, providing handwriting pad)

微信云开发粗糙理解
随机推荐
Implementing fillet in custom dialog
Opencvsharp4 handwriting recognition
Detailed explanation of UCI datasets and their data processing (with 148 datasets and processing codes attached)
House raiding
[data analysis and visualization] key points of data drawing 4- problems of pie chart
Graph theory, tree based concept
Opencvsharp4 pixel read / write and memory structure of color image and gray image
JS deconstruction assignment
After idea uses c3p0 connection pool to connect to SQL database, database content cannot be displayed
Pycharm installation pyqt5 and its tools (QT designer, pyuic, pyrcc) detailed tutorial
Advanced stair climbing
Rough understanding of wechat cloud development
Detailed explanation of handwritten numeral recognition based on support vector machine (Matlab GUI code, providing handwriting pad)
[data and Analysis Visualization] D3 introductory tutorial 2- building shapes in D3
Linear, integer, nonlinear, dynamic programming
Leetcode 926. Flip string to monotonically increasing [prefix and]
Detailed installation tutorial of MATLAB r2019 B-mode ultrasound (complete installation files are attached)
wx.createSelectorQuery()在components获取Dom节点的使用
Using binary heap to implement priority queue
Retrofit easy to use