链表问题(7)----相交

一、题目:两个单链表相交的一系列问题

思路:

1、首先分别判断两个链表是否有环。如果一个有环,一个无环,则表明肯定不相交。

     思路:设置两个指针,一个慢slow【每次只走一步】,一个快fast【每次走两步】,如果快的走完全程,即走到None,则表示没有环,如果两个指针一直走下去,且存在slow  = fast 相等的时候,表明有环。一个发现:

两者相等的时候是头结点,可以试着证明以下:

2、分为两个问题:

(1)两个链表都无环,求相交点或者不相交。

步骤1:获得两个链表的长度len1和len2,以及两个链表的尾部end1和end2。

步骤2:如果两个尾部end1和end2不等,则表明两个链表是没有交点的。

步骤3:如果end1 == end2,则比较两个长度len1和len2,将较长的链表先走 abs(len2-len1)步,然后将两个链表置于同一起点,直到两个链表走到相同的交点。

(2)两个链表都有环,求相交点或者不相交

1、判断链表是否有环的代码:

class Node:
    def __init__(self,value):
        self.value = value
        self.next = None
def getLoopNode(head):
    if not head or not head.next or not head.next.next:
        return False
    slow = head.next
    fast = head.next.next
    while slow != fast:
        if not fast.next or fast.next.next == None:
            return False
        slow = slow.next
        fast = fast.next.next
    return True
head = Node(1)
p = head
for i in range(4):
    p.next = Node(i)
    p = p.next
# p.next = head
res = getLoopNode(head)
    

 2、无环链表的交点代码:

class Node:
    def __init__(self,value):
        self.value = value
        self.next = None
def getLoopNode(head1,head2):
    if not head1 or not head2:
        return None
    #len1,len2用来记录两个链表长度
    len1 , len2 = 0,0
    #end1,end2用来表示两个链表结尾
    end1 , end2 = head1 , head2
    #获得链表1结尾,以及长度
    while end1:
        len1 += 1
        end1 = end1.next
    #获得链表2结尾以及长度
    while end2:
        len2 += 1
        end2 = end2.next
    #如果两个结尾不一样,则表明两个链表不相交
    if end1 != end2:
        return None
    # 比较两个链表的长度
    p1 = head1 if len1 > len2 else head2
    p2 = head2 if p1 == head1 else head1
    #然后将较长的先走abs(len2-len2)步,让两个链表同一起点
    for i in range(abs(len2-len1)):
        p1 = p1.next
    #两个链表同时走,直到两个相等即为相交点
    while p1 != p2:
        p1 = p1.next
        p2 = p2.next
    return p1
        


head1 = Node(1)
p = head1
for i in range(2,5):
    p.next = Node(i)
    p = p.next
head2 = Node(4)
p = head1
head2.next = p.next.next 
res = getLoopNode(head1,head2)

猜你喜欢

转载自www.cnblogs.com/Lee-yl/p/9753060.html