当前位置:网站首页>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
边栏推荐
- Oracle modify default temporary tablespace
- Core concepts of microservice architecture
- Oracle job scheduled task usage details
- pycharm Install packages failed
- Interval query through rownum
- Detailed explanation of redis (Basic + data type + transaction + persistence + publish and subscribe + master-slave replication + sentinel + cache penetration, breakdown and avalanche)
- Analysis of redo log generated by select command
- 校园外卖系统 - 「农职邦」微信原生云开发小程序
- SAP ui5 application development tutorial 72 - animation effect setting of SAP ui5 page routing
- [point cloud series] so net: self organizing network for point cloud analysis
猜你喜欢

Lenovo Saver y9000x 2020

Technologie zéro copie

OSS cloud storage management practice (polite experience)

PG SQL intercepts the string to the specified character position
![[point cloud series] foldingnet: point cloud auto encoder via deep grid deformation](/img/c0/cc29ae6948dbe42954cd9da326ef77.png)
[point cloud series] foldingnet: point cloud auto encoder via deep grid deformation

Unified task distribution scheduling execution framework

The interviewer dug a hole for me: how many concurrent TCP connections can a single server have?

Search ideas and cases of large amount of Oracle redo log
![[machine learning] Note 4. KNN + cross validation](/img/a1/5afccedf509eda92a0fe5bf9b6cbe9.png)
[machine learning] Note 4. KNN + cross validation

Window analysis function last_ VALUE,FIRST_ VALUE,lag,lead
随机推荐
Move blog to CSDN
Campus takeout system - "nongzhibang" wechat native cloud development applet
Dolphin scheduler configuring dataX pit records
Use of GDB
Logstash数据处理服务的输入插件Input常见类型以及基本使用
[point cloud series] full revolutionary geometric features
Antd design form verification
Three characteristics of volatile keyword [data visibility, prohibition of instruction rearrangement and no guarantee of operation atomicity]
集简云 x 飞书深诺,助力企业运营部实现自动化办公
[point cloud series] unsupervised multi task feature learning on point clouds
The interviewer dug a hole for me: what's the use of "/ /" in URI?
./gradlew: Permission denied
Analysis of redo log generated by select command
零拷贝技术
MySQL index [data structure + index creation principle]
Oracle clear SQL cache
Apache seatunnel 2.1.0 deployment and stepping on the pit
SAP UI5 应用开发教程之七十二 - SAP UI5 页面路由的动画效果设置
Migrating your native/mobile application to Unified Plan/WebRTC 1.0 API
Core concepts of microservice architecture