LeetCode 160 - Intersection of Two Linked Lists
Difficulty: easy
Problem Description
English (Intersection of Two Linked Lists)
Given the heads of two singly linked-lists headA
and headB
, return the node at which the two lists intersect. If the two linked lists have no intersection at all, return null
.
For example, the following two linked lists begin to intersect at node c1
:
graph LR
node_a1(("a1"))
node_a2(("a2"))
node_b1(("b1"))
node_b2(("b2"))
node_b3(("b3"))
node_c1(("c1"))
node_c2(("c2"))
node_c3(("c3"))
node_a1 ---> node_a2
node_b1 ---> node_b2 ---> node_b3
node_a2 ---> node_c1
node_b3 ---> node_c1
node_c1 ---> node_c2 ---> node_c3
The test cases are generated such that there are no cycles anywhere in the entire linked structure.
Note that the linked lists must retain their original structure after the function returns.
Custom Judge:
The inputs to the judge are given as follows (your program is not given these inputs):
intersectVal
- The value of the node where the intersection occurs. This is0
if there is no intersected node.listA
- The first linked list.listB
- The second linked list.skipA
- The number of nodes to skip ahead inlistA
(starting from the head) to get to the intersected node.skipB
- The number of nodes to skip ahead inlistB
(starting from the head) to get to the intersected node.
The judge will then create the linked structure based on these inputs and pass the two heads, headA
and headB
to your program. If you correctly return the intersected node, then your solution will be accepted.
Example 1:
graph LR
node_a1((4))
node_a2((1))
node_b1((5))
node_b2((6))
node_b3((1))
node_c1((8))
node_c2((4))
node_c3((5))
node_a1 ---> node_a2
node_b1 ---> node_b2 ---> node_b3
node_a2 ---> node_c1
node_b3 ---> node_c1
node_c1 ---> node_c2 ---> node_c3
1 |
|
Example 2:
graph LR
node_a1((1))
node_a2((9))
node_a3((1))
node_b1((3))
node_c1((2))
node_c2((4))
node_a1 ---> node_a2 ---> node_a3
node_b1
node_a3 ---> node_c1
node_b1 ---> node_c1
node_c1 ---> node_c2
1 |
|
Example 3:
graph LR
node_a1((2))
node_a2((6))
node_a3((4))
node_b1((1))
node_b2((5))
node_a1 ---> node_a2 ---> node_a3
node_b1 ---> node_b2
1 |
|
Constraints:
- The number of nodes of
listA
is in them
. - The number of nodes of
listB
is in then
. 1 <= m, n <= 3 * 10^4
1 <= Node.val <= 10^5
0 <= skipA < m
0 <= skipB < n
intersectVal
is0
iflistA
andlistB
do not intersect.intersectVal == listA[skipA] == listB[skipB]
iflistA
andlistB
intersect.
Follow up: Could you write a solution that runs in O(m + n)
time and use only O(1)
memory?
Chinese (相交链表)
给你两个单链表的头节点 headA
和 headB
,请你找出并返回两个单链表相交的起始节点。如果两个链表不存在相交节点,返回 null
。
图示两个链表在节点 c1
开始相交:
graph LR
node_a1(("a1"))
node_a2(("a2"))
node_b1(("b1"))
node_b2(("b2"))
node_b3(("b3"))
node_c1(("c1"))
node_c2(("c2"))
node_c3(("c3"))
node_a1 ---> node_a2
node_b1 ---> node_b2 ---> node_b3
node_a2 ---> node_c1
node_b3 ---> node_c1
node_c1 ---> node_c2 ---> node_c3
题目数据 保证 整个链式结构中不存在环。
注意, 函数返回结果后,链表必须 保持其原始结构 。
自定义评测:
评测系统 的输入如下(你设计的程序 不适用 此输入):
intersectVal
- 相交的起始节点的值。如果不存在相交节点,这一值为 0listA
- 第一个链表listB
- 第二个链表skipA
- 在listA
中(从头节点开始)跳到交叉节点的节点数skipB
- 在listB
中(从头节点开始)跳到交叉节点的节点数
评测系统将根据这些输入创建链式数据结构,并将两个头节点 headA
和 headB
传递给你的程序。如果程序能够正确返回相交节点,那么你的解决方案将被 视作正确答案 。
示例 1:
graph LR
node_a1((4))
node_a2((1))
node_b1((5))
node_b2((6))
node_b3((1))
node_c1((8))
node_c2((4))
node_c3((5))
node_a1 ---> node_a2
node_b1 ---> node_b2 ---> node_b3
node_a2 ---> node_c1
node_b3 ---> node_c1
node_c1 ---> node_c2 ---> node_c3
1 |
|
示例 2:
graph LR
node_a1((1))
node_a2((9))
node_a3((1))
node_b1((3))
node_c1((2))
node_c2((4))
node_a1 ---> node_a2 ---> node_a3
node_b1
node_a3 ---> node_c1
node_b1 ---> node_c1
node_c1 ---> node_c2
1 |
|
示例 3:
graph LR
node_a1((2))
node_a2((6))
node_a3((4))
node_b1((1))
node_b2((5))
node_a1 ---> node_a2 ---> node_a3
node_b1 ---> node_b2
1 |
|
提示:
listA
中节点数目为m
listB
中节点数目为n
1 <= m, n <= 3 * 10^4
1 <= Node.val <= 10^5
0 <= skipA <= m
0 <= skipB <= n
- 如果
listA
和listB
没有交点,intersectVal
为0
- 如果
listA
和listB
有交点,intersectVal == listA[skipA] == listB[skipB]
进阶: 你能否设计一个时间复杂度 O(m + n)
、仅用 O(1)
内存的解决方案?
Solution
1 |
|