Skip to content

Latest commit

 

History

History
16 lines (16 loc) · 778 Bytes

160_相交链表.org

File metadata and controls

16 lines (16 loc) · 778 Bytes

题目

Screen-Pictures/2020-06-07_10-09-21_%E6%88%AA%E5%B1%8F2020-06-07%20%E4%B8%8A%E5%8D%8810.09.16.png

思路

  • 2个指针同步后移,当移到各自最后一个节点时,跳到另一个链表的头节点继续后移,直到相交或者2个指针都为None

code

class Solution:
    def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
        p, q = headA, headB
	#都为None或者相交
        while p!=q:
            p = p.next if p else headB
            q = q.next if q else headA
        return q