当前位置:网站首页>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
边栏推荐
- Special window function rank, deny_ rank, row_ number
- GDB的使用
- About me
- Oracle defines self incrementing primary keys through triggers and sequences, and sets a scheduled task to insert a piece of data into the target table every second
- Failure to connect due to improper parameter setting of Rac environment database node. Troubleshooting
- Tangent space
- Software test system integration project management engineer full truth simulation question (including answer and analysis)
- 面试官给我挖坑:URI中的 “//” 有什么用?
- [indicators] precision, recall
- RAC environment alert log error drop transient type: systp2jw0acnaurdgu1sbqmbryw = = troubleshooting
猜你喜欢
Campus takeout system - "nongzhibang" wechat native cloud development applet
[point cloud series] summary of papers related to implicit expression of point cloud
零拷貝技術
Cross carbon market and Web3 to achieve renewable transformation
Why do you need to learn container technology to engage in cloud native development
[indicators] precision, recall
Set Jianyun x Feishu Shennuo to help the enterprise operation Department realize office automation
切线空间(tangent space)
Interface idempotency problem
Special window function rank, deny_ rank, row_ number
随机推荐
Solution: you have 18 unapplied migration (s) Your project may not work properly until you apply
Resolution: argument 'radius' is required to be an integer
The interviewer dug a hole for me: what's the use of "/ /" in URI?
Special window function rank, deny_ rank, row_ number
Using open to open a file in JNI returns a - 1 problem
Explanation of input components in Chapter 16
ACFs file system creation, expansion, reduction and other configuration steps
Operations related to Oracle partition
Troubleshooting of expdp export error when Oracle table has logical bad blocks
PG library to view the distribution keys of a table in a certain mode
Get the attribute value difference between two different objects with reflection and annotation
AI21 Labs | Standing on the Shoulders of Giant Frozen Language Models(站在巨大的冷冻语言模型的肩膀上)
Use of GDB
About me
Common types and basic usage of input plug-in of logstash data processing service
Example of specific method for TIA to trigger interrupt ob40 based on high-speed counter to realize fixed-point machining action
QT调用外部程序
Detailed explanation of Oracle tablespace table partition and query method of Oracle table partition
Interface idempotency problem
顶级元宇宙游戏Plato Farm,近期动作不断利好频频