当前位置:网站首页>Leetcode? The first common node of two linked lists
Leetcode? The first common node of two linked lists
2022-04-23 13:45:00 【Du Xiaorui】
Title Description
Title address : The first common node of two linked lists
Solution 1 : Violence enumeration
A simple idea is to enumerate each node directly , The time complexity of doing this is O(M*N).
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
for(ListNode pa = headA; pa!=null; pa = pa.next) {
for(ListNode pb = headB; pb!=null; pb = pb.next) {
if(pa==pb) {
return pa;
}
}
}
return null;
}
}
Solution 2 : Hash set
Start from a header node and go through it , Put all nodes into a hash set , Then start traversing from another head node , Traversing to the first node in the hash set is the first node to meet .
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
Set<ListNode> set = new HashSet<ListNode>();
ListNode temp = headA;
while(temp!=null) {
set.add(temp);
temp = temp.next;
}
while(headB!=null) {
if(set.contains(headB)) {
return headB;
}
headB = headB.next;
}
return null;
}
}
Solution 3 : Double pointer
If both pointers are null at the beginning , Direct return null .
Two temporary pointers traverse from two header nodes respectively , After traversing to the end, return to the head node and continue down , The two temporary nodes update one step at a time , One day two nodes will meet .
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode pa=headA, pb=headB;
if(pa==null & pb==null) {
return null;
}
while(pa!=pb) {
pa = pa == null ? headA : pa.next;
pb = pb == null ? headB : pb.next;
}
return pa;
}
}
Method four : Double stack
Maintain two stacks , Add the elements traversed by the two head nodes to the two stacks respectively . Then start to stack at the same time , Until the elements out of the stack at the same time are different , It indicates that the last element out of the stack is the first public node .
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode pa=headA, pb=headB;
Deque<ListNode> d1 = new ArrayDeque<ListNode>(), d2 = new ArrayDeque<ListNode>();
while(pa!=null) {
d1.add(pa);
pa = pa.next;
}
while(pb!=null) {
d2.add(pb);
pb = pb.next;
}
ListNode ans = null;
while(!d1.isEmpty() && !d2.isEmpty() && d1.peekLast()==d2.peekLast()) {
ans = d1.pollLast();
d2.pollLast();
}
return ans;
}
}
版权声明
本文为[Du Xiaorui]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230556584961.html
边栏推荐
- Exemple de méthode de réalisation de l'action d'usinage à point fixe basée sur l'interruption de déclenchement du compteur à grande vitesse ob40 pendant le voyage de tia Expo
- Troubleshooting of expdp export error when Oracle table has logical bad blocks
- Common types and basic usage of input plug-in of logstash data processing service
- Part 3: docker installing MySQL container (custom port)
- innobackupex增量备份
- Analysis of the problem that the cluster component GIPC in RAC environment cannot correctly identify the heartbeat network state
- Processbuilder tool class
- Why do you need to learn container technology to engage in cloud native development
- The interviewer dug a hole for me: what's the use of "/ /" in URI?
- Django::Did you install mysqlclient?
猜你喜欢
顶级元宇宙游戏Plato Farm,近期动作不断利好频频
零拷贝技术
Apache seatunnel 2.1.0 deployment and stepping on the pit
QT calling external program
MySQL [read / write lock + table lock + row lock + mvcc]
Tersus notes employee information 516 MySQL query (time period uniqueness judgment of 2 fields)
JUC interview questions about synchronized, ThreadLocal, thread pool and atomic atomic classes
Common types and basic usage of input plug-in of logstash data processing service
Ai21 labs | standing on the shoulders of giant frozen language models
【视频】线性回归中的贝叶斯推断与R语言预测工人工资数据|数据分享
随机推荐
Processing of ASM network not automatically started in 19C
TCP 复位gongji原理和实战复现
Django::Did you install mysqlclient?
TIA博途中基於高速計數器觸發中斷OB40實現定點加工動作的具體方法示例
爱可可AI前沿推介 (4.23)
Analysis of the problem that the cluster component GIPC in RAC environment cannot correctly identify the heartbeat network state
Common analog keys of ADB shell: keycode
为什么从事云原生开发需要学习容器技术
[andorid] realize SPI communication between kernel and app through JNI
Isparta is a tool that generates webp, GIF and apng from PNG and supports the transformation of webp, GIF and apng
Error 403 in most cases, you or one of your dependencies are requesting
Generate 32-bit UUID in Oracle
Solve tp6 download error course not find package topthink / think with stability stable
Modification of table fields by Oracle
19c environment ora-01035 login error handling
The interviewer dug a hole for me: how many concurrent TCP connections can a single server have?
MySQL index [data structure + index creation principle]
10g database cannot be started when using large memory host
Oracle database recovery data
交叉碳市场和 Web3 以实现再生变革