如上图,这两个链表相交于结点5,要求判断两个链表是否相交,若是相交,找出相交处的结点。node
如上图所示,若是两个链表相交,那么它们必定会有公共的结点,因为结点的地址或引用能够做为结点的惟一标识,所以,能够经过判断两个链表中的结点是否有相同的地址或引用来判断链表是否相交。指针
将这两个链表首尾相连(例如把链表headl尾结点连接到head2的头指针),而后检测这个链表是否存在环,若是存在,则两个链表相交,而环入口结点即为相交的结点。
code
若是两个链表相交,那么两个链表从相交点到链表结束都是相同的结点,必然是Y字形,因此,判断两个链表的最后一个结点是否是相同便可。即先遍历一个链表,直到尾部,再遍历另一个链表,若是也能够走到一样的结尾点,则两个链表相交,这时记下两个链衰的长度c1,c2,再遍历一次,长链表结点先出发前进|c1-c2|步,以后两个链表同时前进,每次一步,相遇的第一点即为两个链表相交的第一个点。blog
# -*-coding:utf-8-*- """ @Author : 图南 @Software: PyCharm @Time : 2019/9/7 21:24 """ # 尾节点法 class Node: def __init__(self, data=None, next=None): self.data = data self.next = next def printLink(head): if head is None or head.next is None: return cur = head.next while cur is not None: print(cur.data, end=' ') cur = cur.next print() # 构造相交链表 def conInterLink(n1, n2, k): k = int(k) nums1 = list(map(int, n1.split(' '))) nums2 = list(map(int, n2.split(' '))) if len(nums1) > len(nums2): return conLink(nums1, nums2, k) else: head2, head1 = conLink(nums2, nums1, k) return head1, head2 def conLink(nums1, nums2, k): head1 = Node() head2 = Node() cur1 = head1 cur2 = head2 f = None for i in range(1, len(nums1) + 1): node = Node(nums1[i - 1]) cur1.next = node cur1 = node if i == k: f = cur1 for i in range(1, len(nums2) + 1): node = Node(nums2[i - 1]) cur2.next = node cur2 = node cur2.next = f return head1, head2 # 遍历两个链表并计算长度 def traLink(head): cur = head.next count = 1 while cur.next is not None: cur = cur.next count += 1 return cur, count # 长链表先走k步 def beforLink(head, k): cur = head.next for i in range(k): cur = cur.next return cur # 获取相交点 def getPoint(head1, head2, count1, count2): cur1 = beforLink(head1, count1 - count2) cur2 = head2.next while cur1 is not None: if cur1 is cur2: return cur1.data cur1 = cur1.next cur2 = cur2.next def findInterPoint(head1, head2): cur1, count1 = traLink(head1) cur2, count2 = traLink(head2) if cur1 is cur2: print("两个链表相交!") if count1 > count2: return getPoint(head1, head2, count1, count2) else: return getPoint(head2, head1, count2, count1) else: return "两个链表不相交!" if __name__ == '__main__': n1 = input('Link1:') n2 = input('Link2:') k = input('相交点:') head1, head2 = conInterLink(n1, n2, k) printLink(head1) printLink(head2) print(findInterPoint(head1, head2))
链表一长度大于链表二:
链表二长度大于链表一:
两链表不相交:
两链表相交在第一个节点:
utf-8