当前位置:网站首页>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
边栏推荐
- What does the SQL name mean
- 零拷贝技术
- Information: 2021 / 9 / 29 10:01 - build completed with 1 error and 0 warnings in 11S 30ms error exception handling
- SAP ui5 application development tutorial 72 - animation effect setting of SAP ui5 page routing
- Oracle database recovery data
- Migrating your native/mobile application to Unified Plan/WebRTC 1.0 API
- Analysis of cluster component gpnp failed to start successfully in RAC environment
- Longitude and latitude position of provincial capitals in China
- 面试官给我挖坑:URI中的 “//” 有什么用?
- Oracle index status query and index reconstruction
猜你喜欢
交叉碳市场和 Web3 以实现再生变革
聯想拯救者Y9000X 2020
Common types and basic usage of input plug-in of logstash data processing service
PG SQL intercepts the string to the specified character position
浅谈js正则之test方法bug篇
[Video] Bayesian inference in linear regression and R language prediction of workers' wage data | data sharing
Three characteristics of volatile keyword [data visibility, prohibition of instruction rearrangement and no guarantee of operation atomicity]
OSS cloud storage management practice (polite experience)
Lenovo Savior y9000x 2020
[point cloud series] learning representations and generative models for 3D point clouds
随机推荐
[point cloud series] unsupervised multi task feature learning on point clouds
JUC interview questions about synchronized, ThreadLocal, thread pool and atomic atomic classes
At the same time, the problems of height collapse and outer margin overlap are solved
Lenovo Savior y9000x 2020
Oracle creates tablespaces and modifies user default tablespaces
kettle庖丁解牛第16篇之输入组件周边讲解
MySQL [read / write lock + table lock + row lock + mvcc]
Processbuilder tool class
Ai21 labs | standing on the shoulders of giant frozen language models
Oracle clear SQL cache
Use of GDB
Innobackupex incremental backup
Analysis of the problem that the cluster component GIPC in RAC environment cannot correctly identify the heartbeat network state
GDB的使用
TIA博途中基於高速計數器觸發中斷OB40實現定點加工動作的具體方法示例
Cross carbon market and Web3 to achieve renewable transformation
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
Test the time required for Oracle library to create an index with 7 million data in a common way
Database transactions
Oracle RAC database instance startup exception analysis IPC send timeout