尋找鏈表相交結點問題
作者:Grey
原文地址:
題目描述
給定兩個可能有環也可能無環的單鏈表,頭節點head1和head2。請實現一個函數,如果兩個鏈表相交,請返回相交的 第一個節點。如果不相交,返回 null。
要求:如果兩個鏈表長度之和爲N,時間複雜度請達到O(N)
,額外空間複雜度請達到O(1)
。
類似問題
本題主要的難點是要分析所有可能的情況,因爲題目中提到「可能有環也可能無環」。
主要思路
先看大的情況,有如下三種情況
第一種情況:兩個鏈表均無環;
第二種情況:兩個鏈表均有環;
第三種情況:一個有環,一個無環。
首先,第三種情況下,兩個鏈表一定不相交。針對第一種情況,就是尋找鏈表的入環節點和相交節點問題中提到LeetCode 160. Intersection of Two Linked Lists,現在只分析第二種情況。
由於兩個鏈表都有環,兩個鏈表如果相交,一定只有如下三種情況
情況1:兩個鏈表獨立不相交
情況2:兩個鏈表的入環結點是同一個
情況3:兩個鏈表的入環結點不是同一個,此時任意一個鏈表的入環結點都是相交結點。
先從最簡單的情況1和情況3進行分析,情況一發生的條件是:兩個鏈表的入環結點(loop1,loop2)不是同一個,判斷條件很簡單,就是從任意一個鏈表的入環結點開始遍歷一圈,如果都沒有遇到另外一個鏈表的入環結點, 兩個鏈表不相交,屬於情況1;
如果從任意一個鏈表的入環結點開始遍歷一圈,遇到了另外一個鏈表的入環結點,則說明兩個鏈表相交,屬於情況3,且任意一個鏈表的入環結點都是相交結點。
最後分析情況2,兩個鏈表的入環結點如果是同一個,可以記錄兩個鏈表的差值,然後讓短鏈表先走差值步以後,長短鏈表同時開始走,相遇的結點就是第一個相交結點。
完整代碼見:
public class Code_FindFirstIntersectNode {
public static class List {
public int val;
public List next;
public List(int v) {
val = v;
}
}
public static List getIntersectNode(List head1, List head2) {
if (head1 == null || head2 == null) {
return null;
}
// 兩個均無環
List loop1 = getLoopNode(head1);
List loop2 = getLoopNode(head2);
if (loop1 == null && loop2 == null) {
return noLoop(head1, head2);
}
// 兩個均有環
if (loop1 != null && loop2 != null) {
return bothLoop(head1, loop1, head2, loop2);
}
// 一個有環一個無環 ,不可能相交
return null;
}
// 找到鏈表第一個入環節點,如果無環,返回null
public static List getLoopNode(List head) {
if (head == null || head.next == null || head.next.next == null) {
return null;
}
// 慢指針 在第一個節點位置
List slow = head.next;
// 快指針,在第二個節點的位置
List fast = head.next.next;
while (slow != fast) {
if (fast.next == null || fast.next.next == null) {
return null;
}
// 快指針每次走兩步
fast = fast.next.next;
// 慢指針每次走一步
slow = slow.next;
}
// 兩個指針遇上了,說明有環
// 讓快指針回到頭部, 慢指針停在原地
fast = head;
while (fast != slow) {
fast = fast.next;
slow = slow.next;
}
// 快指針每次走一步,慢指針每次走一步,遇上後,就是入環節點處
return slow;
}
// 如果兩個鏈表都無環,返回第一個相交節點,如果不想交,返回null
public static List noLoop(List head1, List head2) {
if (head1 == null || head1 == null) {
return null;
}
// 判斷兩個鏈表的長度
int n = 0;
List t1 = head1;
List t2 = head2;
while (t1.next != null) {
n++;
t1 = t1.next;
}
while (t2.next != null) {
n--;
t2 = t2.next;
}
// 兩個鏈表的末節點不相等
if (t2 != t1) {
return null;
}
// 記錄長的鏈表頭節點
List longer = n > 0 ? head1 : head2;
// 記錄短的鏈表頭節點
List shorter = longer == head1 ? head2 : head1;
// 先讓長鏈表走一段距離(這段的長度就是長鏈表和短鏈表的長度差)
int gap = Math.abs(n);
while (gap != 0) {
gap--;
longer = longer.next;
}
// 然後長鏈表和短鏈表同時開始走,直到相等的節點即爲交點
while (longer != shorter) {
longer = longer.next;
shorter = shorter.next;
}
return shorter;
}
// 兩個有環鏈表,返回第一個相交節點,如果不想交返回null
public static List bothLoop(List head1, List loop1, List head2, List loop2) {
// 只有兩種情況
if (loop1 == loop2) {
// 1. 未入環就相交
// 這種情況下,兩個鏈表的入環節點是一樣
int n = 0;
List t1 = head1;
List t2 = head2;
while (t1 != loop1) {
n++;
t1 = t1.next;
}
while (t2 != loop2) {
n--;
t2 = t2.next;
}
List longer = n > 0 ? head1 : head2;
List shorter = longer == head1 ? head2 : head1;
n = Math.abs(n);
while (n != 0) {
n--;
longer = longer.next;
}
while (longer != shorter) {
longer = longer.next;
shorter = shorter.next;
}
return shorter;
} else {
// 2. 共用環,不在入環處相交,隨便一個鏈表的入環點就是交點
// loop1 != loop2
// 從loop1開始,轉一圈回到loop1
// 如果都沒有遇到loop2,則不相交
// 如果遇到了loop1,則交點爲loop1或者loop2都可以
List t1 = loop1.next;
while (t1 != loop1) {
if (t1 == loop2) {
return loop1;
}
t1 = t1.next;
}
return null;
}
}
}