赞
踩
- 输入:intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3
- 输出:Intersected at '8'
-
- 输入:intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
- 输出:Intersected at '2'
-
- 输入:intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
- 输出:null
- # 双指针
- class Solution:
- def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> Optional[ListNode]:
- A = headA
- B = headB
- while A and B:
- if A == B:
- return A
- A = A.next
- B = B.next
- if A is None and B is None:
- return None
- if A is None:
- A = headB
- if A is None:
- B = headA
- # 另外一种写法
- class Solution:
- def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> Optional[ListNode]:
- A, B = headA, headB
- while A != B:
- A = A.next if A else headB
- B = B.next if B else headA
- return A
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。