LeetCode 0160 Intersection of Two Linked Lists
作者:互联网
1. 题目描述
2. Solution 1
1、思路分析
设len(A) = m, len(B) = n; A与B共同的部分len = s;
m1 = m - s; n1 = n - s;
本题的关键是如何 处理 一般情况下: m1 != n1
方法一: 方案: 求和
鉴于 (m1 + s) + n1 = m + n1 = m1 + ( s + n1) = m1 + n
于是,以headA为出发点的pA走过 m + n1, 以headB为出发点的pB走过 n + m1,pA与pB即可在交叉点相遇
2、代码实现
package Q0199.Q0160IntersectionofTwoLinkedLists;
import DataStructure.ListNode;
public class Solution {
/*
设len(A) = m, len(B) = n; A与B共同的部分len = s;
m1 = m - s; n1 = n - s;
本题的关键是如何 处理 一般情况下: m1 != n1
方法一: 方案: 求和
鉴于 (m1 + s) + n1 = m + n1 =
m1 + ( s + n1) = m1 + n
于是,以headA为出发点的pA走过 m + n1, 以headB为出发点的pB走过 n + m1,pA与pB即可在交叉点相遇
*/
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if (headA == null || headB == null) return null;
ListNode pA = headA, pB = headB;
while (pA != pB) {
pA = pA != null ? pA.next : headB;
pB = pB != null ? pB.next : headA;
}
return pA;
}
}
3、复杂度分析
时间复杂度: O(n)
空间复杂度: O(1)
3. Solution 2
1、思路分析
方法二: 方案: 作差
设 L = max(m, n), S = min(m, n) -> d = L - S
长度为L的链表先走d步,然后与长度为S的链表齐步走,即可相遇
2、代码实现
package Q0199.Q0160IntersectionofTwoLinkedLists;
import DataStructure.ListNode;
public class Solution2 {
/*
方法二: 方案: 作差
设 L = max(m, n), S = min(m, n) -> d = L - S
长度为L的链表先走d步,然后与长度为S的链表齐步走,即可相遇
*/
public int getLength(ListNode head) {
int len = 0;
while (head != null) {
++len;
head = head.next;
}
return len;
}
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if (headA == null || headB == null) return null;
int m = getLength(headA), n = getLength(headB);
ListNode p, q;
for (p = headA; m > n; m--) p = p.next;
for (q = headB; m < n; n--) q = q.next;
while (p != null && q != null && p != q) {
p = p.next;
q = q.next;
}
return (p != null && q != null) ? p : null;
}
}
3、复杂度分析
时间复杂度: O(n)
空间复杂度: O(1)
标签:ListNode,0160,Two,Lists,pA,m1,headB,n1,null 来源: https://www.cnblogs.com/junstat/p/16310665.html