如何判断两个双向链表是否相交及相交点位置

2026-08-26 0 阅读

双向链表是一种常见的线性数据结构,每个节点包含两个指针,分别指向前一个节点和后一个节点。在某些情况下,两个双向链表可能会在某个节点处相交。判断两个双向链表是否相交,以及相交点的位置,是数据结构中一个有趣且具有挑战性的问题。

相交点的判断

要判断两个双向链表是否相交,以及相交点的位置,我们可以使用以下几种方法:

方法一:哈希表法

  1. 遍历第一个链表,并将每个节点的地址存储在一个哈希表中。
  2. 遍历第二个链表,检查每个节点的地址是否在哈希表中。如果发现一个节点的地址在哈希表中,那么这两个链表在相应的节点处相交。
  3. 如果在遍历第二个链表的过程中,我们找到了一个共同节点,那么记录下该节点,这就是两个链表的相交点。

代码示例

class Node:
    def __init__(self, data):
        self.data = data
        self.next = None
        self.prev = None

def detect_intersection_with_hash(head1, head2):
    hash_set = set()
    current = head1
    while current:
        hash_set.add(current)
        current = current.next

    current = head2
    while current:
        if current in hash_set:
            return current
        current = current.next

    return None

方法二:快慢指针法

  1. 初始化两个指针,分别指向两个链表的头部。
  2. 同时遍历两个链表,一个指针每次移动一个节点,另一个指针每次移动两个节点。
  3. 如果两个指针相遇,则存在一个交点。如果其中一个指针到达链表末尾,则两个链表不相交。

代码示例

def detect_intersection_with_floyd(head1, head2):
    slow = head1
    fast = head1

    while fast and fast.next:
        slow = slow.next
        fast = fast.next.next

    if fast is None:
        return None

    slow = head1
    while slow != fast:
        slow = slow.next
        fast = fast.next

    return slow

方法三:反转链表法

  1. 反转第二个链表
  2. 使用哈希表法或快慢指针法检查反转后的链表与第一个链表是否相交。
  3. 如果相交,反转第二个链表回到原来的状态。

代码示例

def detect_intersection_with_reverse(head1, head2):
    if not head2:
        return None

    head2 = reverse_list(head2)
    intersection = detect_intersection_with_hash(head1, head2)
    if intersection:
        head2 = reverse_list(head2)
    return intersection

def reverse_list(head):
    prev = None
    current = head
    while current:
        next_node = current.next
        current.next = prev
        prev = current
        current = next_node
    return prev

总结

以上三种方法都可以用来判断两个双向链表是否相交以及相交点的位置。哈希表法简单易懂,但空间复杂度较高;快慢指针法时间复杂度较低,但需要额外的空间来存储节点地址;反转链表法则可以在不增加额外空间的情况下判断相交点。

根据实际应用场景选择合适的方法,可以帮助我们高效地解决问题。

分享到: