编写程序以查找两个单链表的交点开始的节点。
Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
Output: Reference of the node with value = 8
相交节点的值为8(请注意,如果两个列表相交,则该值不得为0)
因为说了intersect Value不能为0所以line3,4给注释掉了。
代码下面有程序运行过程,方便理解测试值是图里给出的listlink
class Solution:
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
# if not headA or not headB: if None in (headA,headB):
# return None
a=headA
b=headB
while a is not b:
if a:
a=a.next
else:
a=headB
if b:
b=b.next
else:
b&