【LeetCode每日一题】——160.相交链表
文章目录
- 一【题目类别】
- 二【题目难度】
- 三【题目编号】
- 四【题目描述】
- 五【题目示例】
- 六【题目提示】
- 七【题目进阶】
- 八【解题思路】
- 九【时间频度】
- 十【代码实现】
- 十一【提交结果】
一【题目类别】
二【题目难度】
- 简单
三【题目编号】
- 160.相交链表
四【题目描述】
-
给你两个单链表的头节点headA 和headB ,请你找出并返回两个单链表相交的起始节点。如果两个链表不存在相交节点,返回null。
图示两个链表在节点 c1 开始相交:
题目数据 保证 整个链式结构中不存在环。
注意,函数返回结果后,链表必须 保持其原始结构 。
自定义评测:
评测系统 的输入如下(你设计的程序 不适用 此输入):- intersectVal - 相交的起始节点的值。如果不存在相交节点,这一值为 0
- listA - 第一个链表
- listB - 第二个链表
- skipA - 在 listA 中(从头节点开始)跳到交叉节点的节点数
- skipB - 在 listB 中(从头节点开始)跳到交叉节点的节点数
-
评测系统将根据这些输入创建链式数据结构,并将两个头节点 headA 和 headB 传递给你的程序。如果程序能够正确返回相交节点,那么你的解决方案将被 视作正确答案 。
五【题目示例】
-
示例 1:
输入:intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3
输出:Intersected at ‘8’
解释:相交节点的值为 8 (注意,如果两个链表相交则不能为 0)。
从各自的表头开始算起,链表 A 为 [4,1,8,4,5],链表 B 为 [5,6,1,8,4,5]。
在 A 中,相交节点前有 2 个节点;在 B 中,相交节点前有 3 个节点。 -
示例 2:
输入:intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
输出:Intersected at ‘2’
解释:相交节点的值为 2 (注意,如果两个链表相交则不能为 0)。
从各自的表头开始算起,链表 A 为 [1,9,1,2,4],链表 B 为 [3,2,4]。
在 A 中,相交节点前有 3 个节点;在 B 中,相交节点前有 1 个节点。 -
示例 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 。
六【题目提示】
- listA 中节点数目为 m
- listB 中节点数目为 n
- 1 < = m , n < = 3 ∗ 104 1 <= m, n <= 3 * 10^4 1<=m,n<=3∗104
- 1 < = N o d e . v a l < = 105 1 <= Node.val <= 10^5 1<=Node.val<=105
- 0 <= skipA <= m
- 0 <= skipB <= n
- 如果 listA 和 listB 没有交点,intersectVal 为 0
- 如果 listA 和 listB 有交点,intersectVal == listA[skipA] == listB[skipB]
七【题目进阶】
- 你能否设计一个时间复杂度 O(m + n) 、仅用 O(1) 内存的解决方案?
八【解题思路】
- 我的思路是先求两个链表的长度,然后做差,这个差值就是两个链表相差的个数,后面有用。然后然长的链表先走刚才求出的差步,这样两个链表就在同一个起跑线了,然后再一个一个去寻找,就找到了。这样做满足题目进阶要求的时间复杂度和空间复杂度
九【时间频度】
- 时间复杂度:O ( m + n ) O(m+n) O(m+n),其中m 、 n m、n m、n为链表长度
- 空间复杂度:O ( 1 ) O(1) O(1)
十【代码实现】
- Java语言版
package LinkedList;public class p160_IntersectingLinkedLists { int val; p160_IntersectingLinkedLists next; public p160_IntersectingLinkedLists() { } public p160_IntersectingLinkedLists(int val) { this.val = val; } public p160_IntersectingLinkedLists(int val, p160_IntersectingLinkedLists next) { this.val = val; this.next = next; } public static void main(String[] args) { p160_IntersectingLinkedLists nodeA1 = new p160_IntersectingLinkedLists(1); p160_IntersectingLinkedLists nodeA2 = new p160_IntersectingLinkedLists(9); p160_IntersectingLinkedLists nodeA3 = new p160_IntersectingLinkedLists(1); p160_IntersectingLinkedLists nodeC1 = new p160_IntersectingLinkedLists(2); p160_IntersectingLinkedLists nodeC2 = new p160_IntersectingLinkedLists(4); p160_IntersectingLinkedLists nodeB1 = new p160_IntersectingLinkedLists(3); nodeA1.next = nodeA2; nodeA2.next = nodeA3; nodeA3.next = nodeC1; nodeC1.next = nodeC2; nodeB1.next = nodeC1; p160_IntersectingLinkedLists res = getIntersectionNode(nodeA1, nodeB1); System.out.println("Intersected at:" + res.val); } public static p160_IntersectingLinkedLists getIntersectionNode(p160_IntersectingLinkedLists headA, p160_IntersectingLinkedLists headB) { p160_IntersectingLinkedLists p = headA, q = headB; int lenA = 0, lenB = 0, sub; while (p != null) { lenA++; p = p.next; } while (q != null) { lenB++; q = q.next; } p = headA; q = headB; sub = (lenA - lenB) >= 0 ? lenA - lenB : lenB - lenA; if (lenA > lenB) { while (sub != 0) { p = p.next; sub--; } } else { while (sub != 0) { q = q.next; sub--; } } while (p != null && q != null) { if (p == q) { return p; } p = p.next; q = q.next; } return null; }}
- C语言版
#includestruct ListNode{int val;struct ListNode *next;};struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB){struct ListNode *p = headA, *q = headB;int lenA = 0, lenB = 0, sub;while (p != NULL){lenA++;p = p->next;}while (q != NULL){lenB++;q = q->next;}p = headA;q = headB;sub = (lenA - lenB) >= 0 ? lenA - lenB : lenB - lenA;if (lenA > lenB){while (sub != 0){p = p->next;sub--;}}else{while (sub != 0){q = q->next;sub--;}}while (q != NULL && p != NULL){if (p == q){return p;}p = p->next;q = q->next;}return NULL;}/*主函数省略*/
十一【提交结果】
-
Java语言版
-
C语言版