leetcode160相交链表

class Solution:
    def getIntersectionNode(self, head1, head2):
        it1 = head1
        it2 = head2
        # 利用的原理就是两条不相等的线段加起来的值是一个定值, 让两个指针一起来走就好了,走过的路程是一样的
        while it1 and it2:
            if it1.val != it2.val:
                it1 = it1.next
                it2 = it2.next
                if it1 is None and it2 is None:
                    return None
                if it1 is None:
                    it1 = head2
                if it2 is None:
                    it2 = head1
            else:
                return it1
        return None

猜你喜欢

转载自blog.csdn.net/weixin_36149892/article/details/80385394