10min,一次通过
class Solution(object):
def getIntersectionNode(self, headA:ListNode, headB:ListNode):
"""
:type head1, head1: ListNode
:rtype: ListNode
"""
cnt1,cnt2=0,0
cur=headA
while cur:
cnt1+=1
cur=cur.next
cur=headB
while cur:
cnt2+=1
cur=cur.next
while cnt1>cnt2:
headA=headA.next
cnt1-=1
while cnt1<cnt2:
headB=headB.next
cnt2-=1
while headA and headB!=headA: # 没有公共节点
headB=headB.next
headA=headA.next
return headA
考虑到了两个都为空,没有公共节点的情况,很好,一次通过
网友评论