1 連結串列問題
面試時連結串列解題的方法論
對於筆試,不用太在乎空間複雜度,一切為了時間複雜度
對於面試,時間複雜度依然放在第一位,但是一定要找到空間最省的方法
1.1 連結串列面試常用資料結構和技巧
1、 使用容器(雜湊表,陣列等)
2、 快慢指標
1.1.1 快慢指標問題
1、 輸入連結串列頭結點,奇數長度返回中點,偶數長度返回上中點
1 3 5 2 7 返回 5;1 3 2 7 返回 3
2、 輸入連結串列頭結點,奇數長度返回中點,偶數長度返回中下點
1 3 5 2 7 返回 5;1 3 2 7 返回 2
3、 輸入連結串列頭結點,奇數長度返回中點前一個,偶數長度返回上中點前一個
1 3 5 2 7 返回 3;1 3 2 7 返回 1
4、 輸入連結串列頭結點,奇數長度返回中點前一個,偶數長度返回下中點前一個
1 3 5 2 7 返回 3;1 3 2 7 返回 3
package class06;
import java.util.ArrayList;
public class Code01_LinkedListMid {
public static class Node {
public int value;
public Node next;
public Node(int v) {
value = v;
}
}
// head 頭
// 1、奇數長度返回中點,偶數長度返回上中點
public static Node midOrUpMidNode(Node head) {
// 沒有點,有一個點,有兩個點的時候都是返回頭結點
if (head == null || head.next == null || head.next.next == null) {
return head;
}
// 連結串列有3個點或以上
// 快慢指標,快指標一次走兩步,慢指標一次走一步
// 快指標走完,慢指標在中點位置
Node slow = head.next;
Node fast = head.next.next;
while (fast.next != null && fast.next.next != null) {
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
// 2、奇數長度返回中點,偶數長度返回中下點
public static Node midOrDownMidNode(Node head) {
if (head == null || head.next == null) {
return head;
}
Node slow = head.next;
Node fast = head.next;
while (fast.next != null && fast.next.next != null) {
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
// 3、奇數長度返回中點前一個,偶數長度返回上中點前一個
public static Node midOrUpMidPreNode(Node head) {
if (head == null || head.next == null || head.next.next == null) {
return null;
}
Node slow = head;
Node fast = head.next.next;
while (fast.next != null && fast.next.next != null) {
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
// 4、奇數長度返回中點前一個,偶數長度返回下中點前一個
public static Node midOrDownMidPreNode(Node head) {
if (head == null || head.next == null) {
return null;
}
if (head.next.next == null) {
return head;
}
Node slow = head;
Node fast = head.next;
while (fast.next != null && fast.next.next != null) {
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
// 筆試可以用這種複雜點的方法,空間複雜度比上面快慢指標要高
public static Node right1(Node head) {
if (head == null) {
return null;
}
Node cur = head;
ArrayList<Node> arr = new ArrayList<>();
while (cur != null) {
arr.add(cur);
cur = cur.next;
}
return arr.get((arr.size() - 1) / 2);
}
public static Node right2(Node head) {
if (head == null) {
return null;
}
Node cur = head;
ArrayList<Node> arr = new ArrayList<>();
while (cur != null) {
arr.add(cur);
cur = cur.next;
}
return arr.get(arr.size() / 2);
}
public static Node right3(Node head) {
if (head == null || head.next == null || head.next.next == null) {
return null;
}
Node cur = head;
ArrayList<Node> arr = new ArrayList<>();
while (cur != null) {
arr.add(cur);
cur = cur.next;
}
return arr.get((arr.size() - 3) / 2);
}
public static Node right4(Node head) {
if (head == null || head.next == null) {
return null;
}
Node cur = head;
ArrayList<Node> arr = new ArrayList<>();
while (cur != null) {
arr.add(cur);
cur = cur.next;
}
return arr.get((arr.size() - 2) / 2);
}
public static void main(String[] args) {
Node test = null;
test = new Node(0);
test.next = new Node(1);
test.next.next = new Node(2);
test.next.next.next = new Node(3);
test.next.next.next.next = new Node(4);
test.next.next.next.next.next = new Node(5);
test.next.next.next.next.next.next = new Node(6);
test.next.next.next.next.next.next.next = new Node(7);
test.next.next.next.next.next.next.next.next = new Node(8);
Node ans1 = null;
Node ans2 = null;
ans1 = midOrUpMidNode(test);
ans2 = right1(test);
System.out.println(ans1 != null ? ans1.value : "無");
System.out.println(ans2 != null ? ans2.value : "無");
ans1 = midOrDownMidNode(test);
ans2 = right2(test);
System.out.println(ans1 != null ? ans1.value : "無");
System.out.println(ans2 != null ? ans2.value : "無");
ans1 = midOrUpMidPreNode(test);
ans2 = right3(test);
System.out.println(ans1 != null ? ans1.value : "無");
System.out.println(ans2 != null ? ans2.value : "無");
ans1 = midOrDownMidPreNode(test);
ans2 = right4(test);
System.out.println(ans1 != null ? ans1.value : "無");
System.out.println(ans2 != null ? ans2.value : "無");
}
}
1.1.2 面試題一:判斷迴文結構
給定一個單連結串列的頭結點head,請判斷該連結串列是否為迴文結構。迴文就是正著輸出和反著輸出結果一樣
- 棧的方法特別簡單(筆試用)
筆試思路,以此把該連結串列放入棧中。再遍歷該連結串列和棧中彈出的數比對,只要有不一樣,就不是迴文
- 改原連結串列的方法需要注意邊界問題(面試用)
快慢指標解法:用快慢指標定位到中點的位置,奇數就是定位到唯一的中點,偶數定位到上中點。然後把中點右半部分加入棧中去,那麼棧中存的是右半部分的逆序。接著從頭遍歷連結串列,棧中有多少個元素,我們就比較多少步,如果有對不上就不是迴文
快慢指標最優解,不使用容器結構(stack),O(1):同樣的找到中點位置,把右半部分指標回指到中點。接著指標1從L位置,指標2從R位置,往中間遍歷。,每步比對,如果有不一樣,則不是迴文。返回答案之前,把中點右邊的指標調整回來
package class06;
import java.util.Stack;
public class Code02_IsPalindromeList {
public static class Node {
public int value;
public Node next;
public Node(int data) {
this.value = data;
}
}
// need n extra space
public static boolean isPalindrome1(Node head) {
// 依次進棧
Stack<Node> stack = new Stack<Node>();
Node cur = head;
while (cur != null) {
stack.push(cur);
cur = cur.next;
}
// 每個元素和棧中比較
while (head != null) {
if (head.value != stack.pop().value) {
return false;
}
head = head.next;
}
return true;
}
// need n/2 extra space
// 中點右側進棧
public static boolean isPalindrome2(Node head) {
if (head == null || head.next == null) {
return true;
}
Node right = head.next;
Node cur = head;
while (cur.next != null && cur.next.next != null) {
right = right.next;
cur = cur.next.next;
}
Stack<Node> stack = new Stack<Node>();
while (right != null) {
stack.push(right);
right = right.next;
}
while (!stack.isEmpty()) {
if (head.value != stack.pop().value) {
return false;
}
head = head.next;
}
return true;
}
// need O(1) extra space
// 不使用容器(stack)的方法
public static boolean isPalindrome3(Node head) {
if (head == null || head.next == null) {
return true;
}
// 慢指標
Node n1 = head;
// 快指標
Node n2 = head;
while (n2.next != null && n2.next.next != null) { // find mid node
n1 = n1.next; // n1 -> mid
n2 = n2.next.next; // n2 -> end
}
// n1 中點
n2 = n1.next; // n2 -> right part first node
n1.next = null; // mid.next -> null
Node n3 = null;
// 右半部逆序指向中點
while (n2 != null) { // right part convert
n3 = n2.next; // n3 -> save next node
n2.next = n1; // next of right node convert
n1 = n2; // n1 move
n2 = n3; // n2 move
}
// 引入n3記錄最後的位置,之後把右半部再逆序回原來的次序
n3 = n1; // n3 -> save last node
n2 = head;// n2 -> left first node
boolean res = true;
while (n1 != null && n2 != null) { // check palindrome
if (n1.value != n2.value) {
res = false;
break;
}
n1 = n1.next; // left to mid
n2 = n2.next; // right to mid
}
n1 = n3.next;
n3.next = null;
// 把右半部分再逆序回來
while (n1 != null) { // recover list
n2 = n1.next;
n1.next = n3;
n3 = n1;
n1 = n2;
}
return res;
}
public static void printLinkedList(Node node) {
System.out.print("Linked List: ");
while (node != null) {
System.out.print(node.value + " ");
node = node.next;
}
System.out.println();
}
public static void main(String[] args) {
Node head = null;
printLinkedList(head);
System.out.print(isPalindrome1(head) + " | ");
System.out.print(isPalindrome2(head) + " | ");
System.out.println(isPalindrome3(head) + " | ");
printLinkedList(head);
System.out.println("=========================");
head = new Node(1);
printLinkedList(head);
System.out.print(isPalindrome1(head) + " | ");
System.out.print(isPalindrome2(head) + " | ");
System.out.println(isPalindrome3(head) + " | ");
printLinkedList(head);
System.out.println("=========================");
head = new Node(1);
head.next = new Node(2);
printLinkedList(head);
System.out.print(isPalindrome1(head) + " | ");
System.out.print(isPalindrome2(head) + " | ");
System.out.println(isPalindrome3(head) + " | ");
printLinkedList(head);
System.out.println("=========================");
head = new Node(1);
head.next = new Node(1);
printLinkedList(head);
System.out.print(isPalindrome1(head) + " | ");
System.out.print(isPalindrome2(head) + " | ");
System.out.println(isPalindrome3(head) + " | ");
printLinkedList(head);
System.out.println("=========================");
head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
printLinkedList(head);
System.out.print(isPalindrome1(head) + " | ");
System.out.print(isPalindrome2(head) + " | ");
System.out.println(isPalindrome3(head) + " | ");
printLinkedList(head);
System.out.println("=========================");
head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(1);
printLinkedList(head);
System.out.print(isPalindrome1(head) + " | ");
System.out.print(isPalindrome2(head) + " | ");
System.out.println(isPalindrome3(head) + " | ");
printLinkedList(head);
System.out.println("=========================");
head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(1);
printLinkedList(head);
System.out.print(isPalindrome1(head) + " | ");
System.out.print(isPalindrome2(head) + " | ");
System.out.println(isPalindrome3(head) + " | ");
printLinkedList(head);
System.out.println("=========================");
head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(2);
head.next.next.next = new Node(1);
printLinkedList(head);
System.out.print(isPalindrome1(head) + " | ");
System.out.print(isPalindrome2(head) + " | ");
System.out.println(isPalindrome3(head) + " | ");
printLinkedList(head);
System.out.println("=========================");
head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(2);
head.next.next.next.next = new Node(1);
printLinkedList(head);
System.out.print(isPalindrome1(head) + " | ");
System.out.print(isPalindrome2(head) + " | ");
System.out.println(isPalindrome3(head) + " | ");
printLinkedList(head);
System.out.println("=========================");
}
}
1.1.3 面試題二:按值劃分單連結串列
將單連結串列按某值劃分成左邊小,中間相等,右邊大的形式
- 把連結串列放入陣列裡,在陣列上做partion(筆試用)
[3, 2, 4, 7, 0, 2, 1]選擇2劃分,基於2對陣列作partion
- 分成小、中、大三部分。再把各個部分之間串起來(面試用)
藉助6個引用變數,不需要容器O(N),且能保證穩定性。小於區域的頭引用,小於區域的尾引用,等於區域的頭引用,等於區域的尾引用,大於區域的頭引用,大於區域的尾引用。依次對比給定的值加入到這三個區域,之後串聯起來
package class06;
public class Code03_SmallerEqualBigger {
public static class Node {
public int value;
public Node next;
public Node(int data) {
this.value = data;
}
}
// 方法1
public static Node listPartition1(Node head, int pivot) {
if (head == null) {
return head;
}
Node cur = head;
int i = 0;
while (cur != null) {
i++;
cur = cur.next;
}
Node[] nodeArr = new Node[i];
i = 0;
cur = head;
for (i = 0; i != nodeArr.length; i++) {
nodeArr[i] = cur;
cur = cur.next;
}
arrPartition(nodeArr, pivot);
for (i = 1; i != nodeArr.length; i++) {
nodeArr[i - 1].next = nodeArr[i];
}
nodeArr[i - 1].next = null;
// 返回頭結點
return nodeArr[0];
}
public static void arrPartition(Node[] nodeArr, int pivot) {
int small = -1;
int big = nodeArr.length;
int index = 0;
while (index != big) {
if (nodeArr[index].value < pivot) {
swap(nodeArr, ++small, index++);
} else if (nodeArr[index].value == pivot) {
index++;
} else {
swap(nodeArr, --big, index);
}
}
}
public static void swap(Node[] nodeArr, int a, int b) {
Node tmp = nodeArr[a];
nodeArr[a] = nodeArr[b];
nodeArr[b] = tmp;
}
// 方法2
public static Node listPartition2(Node head, int pivot) {
Node sH = null; // small head
Node sT = null; // small tail
Node eH = null; // equal head
Node eT = null; // equal tail
Node mH = null; // big head
Node mT = null; // big tail
Node next = null; // save next node
// every node distributed to three lists
while (head != null) {
next = head.next;
head.next = null;
if (head.value < pivot) {
// 小於節點為空,當前節點即做頭又做尾
if (sH == null) {
sH = head;
sT = head;
// 老的尾節點指向當前節點,老的尾變成當前節點
} else {
sT.next = head;
sT = head;
}
} else if (head.value == pivot) {
if (eH == null) {
eH = head;
eT = head;
} else {
eT.next = head;
eT = head;
}
} else {
if (mH == null) {
mH = head;
mT = head;
} else {
mT.next = head;
mT = head;
}
}
head = next;
}
// 小於區域的尾巴,連等於區域的頭,等於區域的尾巴連大於區域的頭
if (sT != null) { // 如果有小於區域
sT.next = eH;
eT = eT == null ? sT : eT; // 下一步,誰去連大於區域的頭,誰就變成eT
}
// 上面的if,不管跑了沒有,et
// all reconnect
if (eT != null) { // 如果小於區域和等於區域,不是都沒有
eT.next = mH;
}
return sH != null ? sH : (eH != null ? eH : mH);
}
public static void printLinkedList(Node node) {
System.out.print("Linked List: ");
while (node != null) {
System.out.print(node.value + " ");
node = node.next;
}
System.out.println();
}
public static void main(String[] args) {
Node head1 = new Node(7);
head1.next = new Node(9);
head1.next.next = new Node(1);
head1.next.next.next = new Node(8);
head1.next.next.next.next = new Node(5);
head1.next.next.next.next.next = new Node(2);
head1.next.next.next.next.next.next = new Node(5);
printLinkedList(head1);
// head1 = listPartition1(head1, 4);
head1 = listPartition2(head1, 5);
printLinkedList(head1);
}
}
1.1.4 面試題三
一種特殊的單連結串列結構如下:
public static class Node {
public int value;
public Node next;
public Node rand;
public Node(int data) {
this.value = data;
}
}
rand指標式單連結串列節點結構中新增加的指標,rand可能指向連結串列中的任意一個節點,也可能為null。給定一個由Node節點型別組成的無環單連結串列節點head。請實現一個函式完成這個連結串列的複製,並返回複製的新連結串列的頭結點。
要求時間複雜度為O(N),額外空間複雜度為O(1)
- 雜湊表方法(筆試推薦)
第一步遍歷,把所有節點加入到Map<Node, Node>表示老節點到克隆出來的節點對映
第二步遍歷,查map找到克隆節點,最後返回頭結點
- 不用雜湊表的方法,人為構造對應關係(面試推薦)
第一步:每個節點遍歷的時候克隆出來一個新的節點加入到當前節點和其next節點的中間
第二步:此時經過第一步所有節點和其克隆節點都是串在一起的,依次拿出當前節點和其克隆節點,當前節點的rand指標指向的節點的克隆節點給當前節點克隆的節點的rand節點指向的節點。
第三步:此時老節點的rand指標沒變化,克隆節點的rand指標也都指向了對應的克隆節點。此時在大的連結串列上分離出來原連結串列和克隆連結串列
package class06;
import java.util.HashMap;
public class Code04_CopyListWithRandom {
public static class Node {
public int value;
public Node next;
public Node rand;
public Node(int data) {
this.value = data;
}
}
// 方法1
public static Node copyListWithRand1(Node head) {
HashMap<Node, Node> map = new HashMap<Node, Node>();
Node cur = head;
while (cur != null) {
// 當前節點,克隆出來一個相同值的新節點加入字典
map.put(cur, new Node(cur.value));
cur = cur.next;
}
// 當前節點從頭開始
cur = head;
while (cur != null) {
// cur 老
// map.get(cur) 新
map.get(cur).next = map.get(cur.next);
map.get(cur).rand = map.get(cur.rand);
cur = cur.next;
}
// 返回head對應的克隆節點
return map.get(head);
}
// 方法二
public static Node copyListWithRand2(Node head) {
if (head == null) {
return null;
}
Node cur = head;
Node next = null;
// 克隆出來的node放在原本node和next指向的node中間
// 1 -> 2
// 1 -> 1' -> 2
while (cur != null) {
// cur 老 next 老的下一個
next = cur.next;
cur.next = new Node(cur.value);
cur.next.next = next;
cur = next;
}
cur = head;
Node curCopy = null;
// set copy node rand
// 1 -> 1' -> 2 -> 2'
// 設定新的克隆節點間的rand節點
while (cur != null) {
// cur 老
// cur.next => 新的 copy出來的節點
next = cur.next.next;
curCopy = cur.next;
curCopy.rand = cur.rand != null ? cur.rand.next : null;
cur = next;
}
// 老的頭結點:head 新克隆出來的頭結點: head.next
Node res = head.next;
cur = head;
// split,分離原本節點組成的連結串列和克隆節點組成的連結串列
while (cur != null) {
next = cur.next.next;
curCopy = cur.next;
cur.next = next;
curCopy.next = next != null ? next.next : null;
cur = next;
}
return res;
}
public static void printRandLinkedList(Node head) {
Node cur = head;
System.out.print("order: ");
while (cur != null) {
System.out.print(cur.value + " ");
cur = cur.next;
}
System.out.println();
cur = head;
System.out.print("rand: ");
while (cur != null) {
System.out.print(cur.rand == null ? "- " : cur.rand.value + " ");
cur = cur.next;
}
System.out.println();
}
public static void main(String[] args) {
Node head = null;
Node res1 = null;
Node res2 = null;
printRandLinkedList(head);
res1 = copyListWithRand1(head);
printRandLinkedList(res1);
res2 = copyListWithRand2(head);
printRandLinkedList(res2);
printRandLinkedList(head);
System.out.println("=========================");
head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(4);
head.next.next.next.next = new Node(5);
head.next.next.next.next.next = new Node(6);
head.rand = head.next.next.next.next.next; // 1 -> 6
head.next.rand = head.next.next.next.next.next; // 2 -> 6
head.next.next.rand = head.next.next.next.next; // 3 -> 5
head.next.next.next.rand = head.next.next; // 4 -> 3
head.next.next.next.next.rand = null; // 5 -> null
head.next.next.next.next.next.rand = head.next.next.next; // 6 -> 4
printRandLinkedList(head);
res1 = copyListWithRand1(head);
printRandLinkedList(res1);
res2 = copyListWithRand2(head);
printRandLinkedList(res2);
printRandLinkedList(head);
System.out.println("=========================");
}
}
1.1.5 面試題四
該問題和約舍夫環問題是連結串列問題的比較難的問題
題目描述:給定兩個可能有環也可能無環的單連結串列,頭結點head1和head2。請實現一個函式,如果兩個連結串列相交,請返回相交的第一個節點。如果不相交,返回null
要求:如果兩個連結串列長度之和為N,時間複雜度請達到O(N),額為空間複雜度請達到O(1)
思路:由於是單連結串列,則一旦成環就結束,出不來,因為每個節點只有一個Next指標
-
方法一:用set把每個節點的記憶體地址放到set裡面,如果存在相同的記憶體在set中存在,則就是第一個成環的節點
-
用快慢指標對連結串列遍歷,那麼快慢指標一定會相遇,能相遇就說明存在環。然後讓慢指標停在相遇的位置,快指標回到頭結點。快指標和慢指標再出發且快指標也變成一次走一步和滿指標相同,再次相遇的節點就是成環節點
package class06;
public class Code05_FindFirstIntersectNode {
public static class Node {
public int value;
public Node next;
public Node(int data) {
this.value = data;
}
}
public static Node getIntersectNode(Node head1, Node head2) {
if (head1 == null || head2 == null) {
return null;
}
// head1的第一個入環節點
Node loop1 = getLoopNode(head1);
// head2的第一個入環節點
Node loop2 = getLoopNode(head2);
// 兩個無環連結串列是否相交的情況
// 由於每個節點只有一個next指標,則如果兩個無環相交則相交之後就只剩下公共部分
// 方法1把第一條連結串列放到set中,第二個連結串列依次查在不在該set中,第一個找到的就是
// 方法2
// 把連結串列1走到尾結點end1,記錄長度l1
// 把連結串列1走到尾結點end2,記錄長度l2
// 如果end1和end2的記憶體地址不同一定不相交
// 如果end1==end2,則(1)長的連結串列從頭結點先走保證和短連結串列相同長度的位置,再以此往下走,第一次相同節點
// (2)則依次從尾結點出發,找第一次出現記憶體地址不相同的那個節點,該節點的next節點就是第一次相交的節點
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 Node getLoopNode(Node head) {
if (head == null || head.next == null || head.next.next == null) {
return null;
}
// n1 慢 n2 快
Node n1 = head.next; // n1 -> slow
Node n2 = head.next.next; // n2 -> fast
while (n1 != n2) {
if (n2.next == null || n2.next.next == null) {
return null;
}
n2 = n2.next.next;
n1 = n1.next;
}
// 能相遇則跳出while,快指標回到開頭,滿指標停在原地
n2 = head; // n2 -> walk again from head
while (n1 != n2) {
// 此時快慢指標每次移動相同步數
n1 = n1.next;
n2 = n2.next;
}
return n1;
}
// 如果兩個連結串列都無環,返回第一個相交節點,如果不想交,返回null
public static Node noLoop(Node head1, Node head2) {
if (head1 == null || head2 == null) {
return null;
}
Node cur1 = head1;
Node cur2 = head2;
int n = 0;
while (cur1.next != null) {
n++;
cur1 = cur1.next;
}
while (cur2.next != null) {
n--;
cur2 = cur2.next;
}
if (cur1 != cur2) {
return null;
}
// n : 連結串列1長度減去連結串列2長度的值
cur1 = n > 0 ? head1 : head2; // 誰長,誰的頭變成cur1
cur2 = cur1 == head1 ? head2 : head1; // 誰短,誰的頭變成cur2
n = Math.abs(n);
while (n != 0) {
n--;
cur1 = cur1.next;
}
while (cur1 != cur2) {
cur1 = cur1.next;
cur2 = cur2.next;
}
return cur1;
}
// 兩個有環連結串列,返回第一個相交節點,如果不想交返回null
// head1的入環節點是loop1,head2的入環節點是loop2
public static Node bothLoop(Node head1, Node loop1, Node head2, Node loop2) {
Node cur1 = null;
Node cur2 = null;
// 類似第一種都無環的情況
if (loop1 == loop2) {
cur1 = head1;
cur2 = head2;
int n = 0;
while (cur1 != loop1) {
n++;
cur1 = cur1.next;
}
while (cur2 != loop2) {
n--;
cur2 = cur2.next;
}
cur1 = n > 0 ? head1 : head2;
cur2 = cur1 == head1 ? head2 : head1;
n = Math.abs(n);
while (n != 0) {
n--;
cur1 = cur1.next;
}
while (cur1 != cur2) {
cur1 = cur1.next;
cur2 = cur2.next;
}
return cur1;
} else {
//否則,找第一個成環節點轉回自身的過程中遇到loop2,則相交,否則不相交
cur1 = loop1.next;
while (cur1 != loop1) {
if (cur1 == loop2) {
return loop1;
}
cur1 = cur1.next;
}
return null;
}
}
public static void main(String[] args) {
// 1->2->3->4->5->6->7->null
Node head1 = new Node(1);
head1.next = new Node(2);
head1.next.next = new Node(3);
head1.next.next.next = new Node(4);
head1.next.next.next.next = new Node(5);
head1.next.next.next.next.next = new Node(6);
head1.next.next.next.next.next.next = new Node(7);
// 0->9->8->6->7->null
Node head2 = new Node(0);
head2.next = new Node(9);
head2.next.next = new Node(8);
head2.next.next.next = head1.next.next.next.next.next; // 8->6
System.out.println(getIntersectNode(head1, head2).value);
// 1->2->3->4->5->6->7->4...
head1 = new Node(1);
head1.next = new Node(2);
head1.next.next = new Node(3);
head1.next.next.next = new Node(4);
head1.next.next.next.next = new Node(5);
head1.next.next.next.next.next = new Node(6);
head1.next.next.next.next.next.next = new Node(7);
head1.next.next.next.next.next.next = head1.next.next.next; // 7->4
// 0->9->8->2...
head2 = new Node(0);
head2.next = new Node(9);
head2.next.next = new Node(8);
head2.next.next.next = head1.next; // 8->2
System.out.println(getIntersectNode(head1, head2).value);
// 0->9->8->6->4->5->6..
head2 = new Node(0);
head2.next = new Node(9);
head2.next.next = new Node(8);
head2.next.next.next = head1.next.next.next.next.next; // 8->6
System.out.println(getIntersectNode(head1, head2).value);
}
}
1.1.6 面試題五
題目描述:能不能不給單連結串列的頭結點,只給想要刪除的節點,就能做到在連結串列上把這個點刪掉?
- 抖機靈的做法,1->2->3->4->5->null,給定3。那麼根據記憶體地址找到3這個節點,把3下個節點賦值給自身變成4,再把自身的下一個指標指向下下個值5即可。1->2->4->5->null。缺點沒把原始節點刪除,只是改變了值,記憶體地址沒被刪掉而是刪掉了需要刪除節點的下一個記憶體地址。該方法無法刪除連結串列的最後一個節點
實質上不給頭結點,無法刪除給定的節點。沒有頭結點,沒法準確的連指標
package class06;
public class Test {
public static class Node{
public int value;
public Node next;
public Node(int v) {
value = v;
}
}
public static void main(String[] args) {
Node a = new Node(1);
Node b = new Node(2);
Node c = new Node(3);
a.next = b;
b.next = c;
// 實質上這裡置為空沒用,只是把Java棧中的變數不指向堆中的3節點
//堆中的結構沒改變,3節點並沒有被刪除
c = null;
}
}