力扣 02.07 链表相交
class Solution:
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
LA = 0
LB = 0
cur_a = headA
while cur_a:
LA += 1
cur_a = cur_a.next
cur_b = headB
while cur_b:
LB += 1
cur_b = cur_b.next
cur_a = headA
cur_b = headB
if LA - LB >= 0:
for i in range(LA - LB):
cur_a = cur_a.next
for i in range(LB):
if cur_a == cur_b:
return cur_a
cur_a = cur_a.next
cur_b = cur_b.next
else:
for i in range(LB - LA):
cur_b = cur_b.next
for i in range(LA):
if cur_a == cur_b:
return cur_a
cur_a = cur_a.next
cur_b = cur_b.next
return None