42 - 相交链表

题目

编写一个程序,找到两个单链表相交的起始节点。

如下面的两个链表:

160_statement

在节点 c1 开始相交。

示例 1:

160_example_1

输入:intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3 输出:Reference of the node with value = 8 输入解释:相交节点的值为 8 (注意,如果两个列表相交则不能为 0)。从各自的表头开始算起,链表 A 为 [4,1,8,4,5],链表 B 为 [5,0,1,8,4,5]。在 A 中,相交节点前有 2 个节点;在 B 中,相交节点前有 3 个节点。

示例 2:

160_example_2

输入:intersectVal = 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 输出:Reference of the node with value = 2 输入解释:相交节点的值为 2 (注意,如果两个列表相交则不能为 0)。从各自的表头开始算起,链表 A 为 [0,9,1,2,4],链表 B 为 [3,2,4]。在 A 中,相交节点前有 3 个节点;在 B 中,相交节点前有 1 个节点。

示例 3:

160_example_3

输入:intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 输出:null 输入解释:从各自的表头开始算起,链表 A 为 [2,6,4],链表 B 为 [1,5]。由于这两个链表不相交,所以 intersectVal 必须为 0,而 skipA 和 skipB 可以是任意值。 解释:这两个链表不相交,因此返回 null。

注意:

  • 如果两个链表没有交点,返回 null.

  • 在返回结果后,两个链表仍须保持原有的结构。

  • 可假定整个链表结构中没有循环。

  • 程序尽量满足 O(n) 时间复杂度,且仅用 O(1) 内存。

解答

Symbol

对之前的“走自己的路让别人无路可走”,念念不忘。

于是第一反应就是用symbol()覆盖掉整个链表,每一步都把值存起来。如果另一个链表循环的时候遇到了相同的值,那么就说明相交了。

虽然这么做吧链表的值给改了,但可以在提交前把值再贴回去呀。

var rewrite = function (temp, headA) {
  let c = headA
  temp.map(e => {
    c.val = e
    c = c.next
  })
}

var getIntersectionNode = function (headA, headB) {
  const flag = Symbol()
  let a = headA
  b = headB
  temp = []
  while (a) {
    temp.push(a.val)
    a.val = flag
    a = a.next
  }
  while (b) {
    if (b.val === flag) {
      rewrite(temp, headA)
      return b
    }
    b = b.next
  }
  rewrite(temp, headA)
  return null
};

Runtime: 100 ms, faster than 21.23% of JavaScript online submissions for Intersection of Two Linked Lists.

Memory Usage: 42.8 MB, less than 78.85% of JavaScript online submissions for Intersection of Two Linked Lists.

研究了一下题解,这种想法倒没啥人想到

作者:mathman-WcD7GSqY1y

链接:https://leetcode-cn.com/problems/two-sum/solution/di-si-chong-jie-fa-li-yong-su-shu-xing-zhi-da-biao/

这个人提到了用素数作为标记,感觉是没有symbol的替代法,似乎也能用随机数吧。

双指针

作者:LeetCode

链接:https://leetcode-cn.com/problems/two-sum/solution/xiang-jiao-lian-biao-by-leetcode/

作者:user7208t

链接:https://leetcode-cn.com/problems/two-sum/solution/tu-jie-xiang-jiao-lian-biao-by-user7208t/

非常巧妙的解法,headA跑完了到 headB的头,headB跑完了到 headA的头。相当于做了a+bb+a的两个链表。

5651993ddb76ae6a42f0b338aec9382206f567041113f49d6ca670832ac75791-Picture1

如果两个表相交了,那么就会返回相交点;如果不相交就会返回null

e86e947c8b87ac723b9c858cd3834f9a93bcc6c5e884e41117ab803d205ef662-相交链表
/**
 * @param {ListNode} headA
 * @param {ListNode} headB
 * @return {ListNode}
 */
var getIntersectionNode = function (headA, headB) {
  if (!headA || !headB) {
    return null
  }
  let a = headA
  b = headB
  while (a !== b) {
    a = a ? a.next : headB
    b = b ? b.next : headA
  }
  return a
}

Runtime: 80 ms, faster than 90.19% of JavaScript online submissions for Intersection of Two Linked Lists.

Memory Usage: 42.9 MB, less than 65.38% of JavaScript online submissions for Intersection of Two Linked Lists.

go

func getIntersectionNode(headA, headB *ListNode) *ListNode {
    if headA == nil || headB == nil {
        return nil
    }
    a, b := headA, headB
    for a != b {
        if a != nil {
            a = a.Next
        } else {
            a = headB
        }
        if b != nil {
            b = b.Next
        } else {
            b = headA
        }
    }
    return a
}

Runtime: 44 ms, faster than 83.11% of Go online submissions forIntersection of Two Linked Lists.

Memory Usage: 8.2 MB, less than 92.86% of Go online submissions forIntersection of Two Linked Lists.

Last updated

Was this helpful?