当前位置:网站首页>Leetcode question brushing series - mode 2 (datastructure linked list) - 160:intersection of two linked list
Leetcode question brushing series - mode 2 (datastructure linked list) - 160:intersection of two linked list
2022-06-11 04:36:00 【Dabie Mountains】
leetcode Brush questions series ---- Pattern 2(Datastructure Linked list )- 160:Intersection of Two Linked List Intersecting list
Tips
- For more information, please refer to the catalogue of this series
- This linked list problem can be solved by using double pointers , Note that the complexity of the algorithm is O(m+n), signify 2*(m+n) Second traversal also belongs to the time complexity , We should not think narrowly that we only traverse m+n Secondary linked list node .
- Double pointers are really a big class of algorithmic ideas .
Python
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
pointA = headA
pointB = headB
if not pointA or not pointB: return None
while pointA != pointB:
if pointA: pointA = pointA.next
else: pointA = headB
if pointB: pointB = pointB.next
else: pointB = headA
return pointA
C++
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
ListNode* pointA = headA;
ListNode* pointB = headB;
if (pointA==nullptr || pointB==nullptr) return nullptr;
while (pointA!=pointB)
{
if(pointA!=nullptr) pointA=pointA->next;
else pointA=headB;
if(pointB!=nullptr) pointB=pointB->next;
else pointB=headA;
}
return pointA;
}
};
C#
/** * Definition for singly-linked list. * public class ListNode { * public int val; * public ListNode next; * public ListNode(int x) { val = x; } * } */
public class Solution {
public ListNode GetIntersectionNode(ListNode headA, ListNode headB) {
ListNode pointA = headA;
ListNode pointB = headB;
if (pointA==null || pointB==null) return null;
while (pointA!=pointB)
{
if(pointA!=null) pointA=pointA.next;
else pointA=headB;
if(pointB!=null) pointB=pointB.next;
else pointB=headA;
}
return pointA;
}
}
Java
/** * 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) {
ListNode pointA = headA;
ListNode pointB = headB;
if (pointA==null || pointB==null) return null;
while (pointA!=pointB)
{
if(pointA!=null) pointA=pointA.next;
else pointA=headB;
if(pointB!=null) pointB=pointB.next;
else pointB=headA;
}
return pointA;
}
}
边栏推荐
- International qihuo: what are the risks of Zhengda master account
- Unity MonoSingleton
- Guanghetong launched a new generation of 3GPP R16 industrial 5g module fg160 engineering sample
- The second discussion class on mathematical basis of information and communication
- Unity的URP的RenderFeature相关编程内容梳理
- 碳路先行,华为数字能源为广西绿色发展注入新动能
- Emlog新版导航源码/带用户中心
- AI helps release legal potential energy! Release of iterms contract intelligent review system
- MySQL stored procedure
- The live broadcast helped Hangzhou e-commerce Unicorn impact the listing, and the ledger system restructured the new pattern of e-commerce transactions
猜你喜欢

Unity Editor Extension save location
![[customview] glide+bitmaptransformation picture upper and lower border wave processing (wavetransformation)](/img/20/6ded07851466d6ef5e5d5a296e3aed.png)
[customview] glide+bitmaptransformation picture upper and lower border wave processing (wavetransformation)

How to quickly find the official routine of STM32 Series MCU

JVM (7): dynamic link, method call, four method call instructions, distinguishing between non virtual methods and virtual methods, and the use of invokedynamic instructions

图的最短路径问题 详细分解版

New UI learning method subtraction professional version 34235 question bank learning method subtraction professional version applet source code

The live broadcast helped Hangzhou e-commerce Unicorn impact the listing, and the ledger system restructured the new pattern of e-commerce transactions

Acts: how to hide defects?

Mathematical basis of information and communication -- the first experiment

The future has come and the 5g advanced era has begun
随机推荐
Unity 可缩放地图的制作
正大国际琪貨:交易市场
Golang generics: generics
梅州植物组培实验室建设资料整理
用万用表检测数码管
International qihuo: what are the risks of Zhengda master account
PHP regular use case
传说使用Shader的ID来设置Shader属性能提高效率:)
Redis主从复制、哨兵、cluster集群原理+实验(好好等,会晚些,但会更好)
如何快速寻找STM32系列单片机官方例程
Decision tree (hunt, ID3, C4.5, cart)
Analysis of hidden dangers in the construction of Fuzhou chemical laboratory
Carbon path first, Huawei digital energy injects new momentum into the green development of Guangxi
Unity 消息框架 NotificationCenter
idea gradle项目 如何导入 本地jar 包
Image detection related model data format
ACTS:高效的测试设计(并赠送一个优秀的测试设计工具)
From the first generation of sowing to the first generation of flowers, 5g commercial "gave birth to all things" for the third anniversary
2022年新高考1卷17题解析
JVM (7): dynamic link, method call, four method call instructions, distinguishing between non virtual methods and virtual methods, and the use of invokedynamic instructions