-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy path相交链表.py
52 lines (46 loc) · 1019 Bytes
/
相交链表.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
编写一个程序,找到两个单链表相交的起始节点。
如下面的两个链表:
# 在节点
# c1
# 开始相交。
#
#
#
# 示例
# 1:
#
#
#
# 输入:intersectVal = 8, listA = [4, 1, 8, 4, 5], listB = [5, 0, 1, 8, 4, 5], skipA = 2, skipB = 3
# 输出:Reference
# of
# the
# node
# with value = 8
# 输入解释:相交节点的值为
# 8 (注意,如果两个列表相交则不能为
# 0)。从各自的表头开始算起,链表
# A
# 为[4, 1, 8, 4, 5],链表
# B
# 为[5, 0, 1, 8, 4, 5]。在
# A
# 中,相交节点前有
# 2
# 个节点;在
# B
# 中,相交节点前有
# 3
# 个节点。
# 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:
ha,hb = headA,headB
while ha != hb:
ha = ha.next if ha else headB
hb = hb.next if hb else headA
return ha