Kodewick
開始學 →
題型與套路:順序表和鏈結串列10 / 50

題型與套路:順序表和鏈結串列

第 10 課,共 50 課 · 第 2 部分 · 順序表和鏈結串列 · 約 18 分鐘

這一章的順序表和鏈結串列總複習講五類題:順序表和鏈結串列怎麼選、頭插法和尾插法建表、用三個指標原地逆置、快慢指標找中間和判環、合併兩個有序鏈結串列。

學完順序表和鏈結串列,期末、考研 408 和麵試最常考的就是這幾類題,這一課一關講一類。每一類記住一個套路,碰到變形也能套上去。

選擇題:順序表還是鏈結串列

先問自己:這份資料最常做的操作是什麼?

最常做的選為什麼
按位置取(第 i 個)順序表O(1);鏈結串列要走 i 步
在已知結點前後插入、刪除鏈結串列O(1);順序表要挪
事先不知道有多少個鏈結串列(或動態陣列)不用一次要一大塊
很在意記憶體、要快取友好順序表不用存指標,連續記憶體讀得快

常見陷阱: 「鏈結串列插入是 O(1)」前提是已經拿到前一個結點;「在第 i 個位置插入」兩種都要 O(n):順序錶慢在挪,鏈結串列慢在找。

單選

要存全班的成績,最常做的是「查第 i 個同學的成績」,幾乎不插入、刪除。選哪個?

頭插法和尾插法:建表

依次讀進 1 2 3 4 5 建一個帶頭結點的鏈結串列,有兩種插法:

  • 頭插法:每個新結點都插在頭結點後面。建出來是反的:5 4 3 2 1。
  • 尾插法:用一個 tail 指標記著最後一個,每次接在它後面。順序不變。
L↓
Hh
3c
2b
1a
NULL

頭插法:每個新結點都插在頭結點 H 後面,所以先來的被擠到後面,讀進 1 2 3,建出來是 3 2 1
L↓tail↓
Hh
1a
2b
3c
NULL

尾插法:tail 一直指著最後一個,新結點接在它後面,順序不變
main.c
1#include <stdio.h>2#include <stdlib.h>3 4typedef struct Node {5    int data;6    struct Node *next;7} Node;8 9Node *new_node(int data, Node *next) {10    Node *n = malloc(sizeof(Node));11    n->data = data;12    n->next = next;13    return n;14}15 16void show(Node *L) {17    for (Node *p = L->next; p != NULL; p = p->next)18        printf("%d ", p->data);19    printf("\n");20}21 22int main(void) {23    // 頭插法:插在頭結點後面24    Node *A = new_node(0, NULL);25    for (int x = 1; x <= 5; x++)26        A->next = new_node(x, A->next);27    show(A);28 29    // 尾插法:接在 tail 後面30    Node *B = new_node(0, NULL);31    Node *tail = B;32    for (int x = 1; x <= 5; x++) {33        tail->next = new_node(x, NULL);34        tail = tail->next;35    }36    show(B);37    return 0;38}
輸出
5 4 3 2 1 
1 2 3 4 5 

所以「把一個鏈結串列逆過來」的一種辦法就是:把結點一個個拆下來,用頭插法插回去。

猜輸出猜錯也沒關係

尾插法忘了挪 tail,印出什麼?

1#include <stdio.h>2#include <stdlib.h>3 4typedef struct Node {5    int data;6    struct Node *next;7} Node;8 9Node *new_node(int data, Node *next) {10    Node *n = malloc(sizeof(Node));11    n->data = data;12    n->next = next;13    return n;14}15 16int main(void) {17    Node *L = new_node(0, NULL);18    Node *tail = L;19    for (int x = 1; x <= 3; x++)20        tail->next = new_node(x, NULL);21    for (Node *p = L->next; p != NULL; p = p->next)22        printf("%d ", p->data);23    printf("\n");24    return 0;25}

原地逆置

不另外要記憶體,把 1 → 2 → 3 改成 3 → 2 → 1。套路是三個指標:prev(已經反好的那段的頭)、cur(正在處理的)、next(先記住下一個,免得斷了找不到)。

cur↓
1a
2b
3c
NULL

原地逆置:每一步把 cur 的箭頭掉過頭,指向 prev
1Node *prev = NULL, *cur = head;2while (cur != NULL) {3    // 1. 先記住下一個4    Node *next = cur->next;5    // 2. 掉頭6    cur->next = prev;7    // 3. 兩個指標都往前挪8    prev = cur;9    cur = next;10}11// 新的頭12head = prev;

每個結點只碰一次:O(n) 時間,O(1) 空間。

排順序拖動或點箭頭排順序

把迴圈裡的這 4 行排好,讓程式把 1 2 3 4 逆置成 4 3 2 1。

  1. prev = cur;
  2. cur = next;
  3. cur->next = prev;
  4. Node *next = cur->next;
要顯示
4 3 2 1
練習 1

原地逆置

補全 reverse:用三個指標把鏈結串列原地逆過來,返回新的頭。

要顯示
4 3 2 1
exercise1.c
1#include <stdio.h>2#include <stdlib.h>3 4typedef struct Node {5    int data;6    struct Node *next;7} Node;8 9Node *new_node(int data, Node *next) {10    Node *n = malloc(sizeof(Node));11    n->data = data;12    n->next = next;13    return n;14}15 16void show(Node *head) {17    for (Node *p = head; p != NULL; p = p->next)18        printf("%d ", p->data);19    printf("\n");20}21 22Node *reverse(Node *head) {23    Node *prev = NULL, *cur = head;24    // 記住下一個、掉頭、兩個都往前挪25    return prev;26}27 28int main(void) {29    Node *head = new_node(1, new_node(2, new_node(3, new_node(4, NULL))));30    head = reverse(head);31    show(head);32    return 0;33}
輸出
點「執行」或按 Ctrl+Enter

快慢指標:找中間、判有環

兩個指標同時從頭出發,slow 每次走 1 步,fast 每次走 2 步。fast 走到頭時,slow 正好在中間。

slow↓fast↓
1a
2b
3c
4d
5e
NULL

快慢指標:fast 一次兩步,slow 一次一步;fast 到頭時 slow 在中間
main.c
1#include <stdio.h>2#include <stdlib.h>3 4typedef struct Node {5    int data;6    struct Node *next;7} Node;8 9Node *new_node(int data, Node *next) {10    Node *n = malloc(sizeof(Node));11    n->data = data;12    n->next = next;13    return n;14}15 16int main(void) {17    Node *head = new_node(1, new_node(2, new_node(3, new_node(4, new_node(5, NULL)))));18    Node *slow = head, *fast = head;19    while (fast != NULL && fast->next != NULL) {20        slow = slow->next;21        fast = fast->next->next;22    }23    printf("中間是 %d\n", slow->data);24    return 0;25}
輸出
中間是 3

同一個套路還能判斷有沒有環:要是鏈結串列某處指回了前面,fast 永遠走不到 NULL,而且總會從後面追上 slow(兩個指標相等)。

slow↓fast↓
1a
2b
3c
4d
5e
NULL

判環:5 指回 3 成了環;slow 一次 1 步,fast 一次 2 步,進了環以後 fast 每輪追近 1 步,一定會追上
單選

用快慢指標判斷有沒有環,時間和額外空間各是多少?

練習 2

有沒有環

補全 has_cycle:用快慢指標判斷鏈結串列有沒有環,有返回 1,沒有返回 0。

要顯示
1 0
exercise2.c
1#include <stdio.h>2#include <stdlib.h>3 4typedef struct Node {5    int data;6    struct Node *next;7} Node;8 9Node *new_node(int data, Node *next) {10    Node *n = malloc(sizeof(Node));11    n->data = data;12    n->next = next;13    return n;14}15 16int has_cycle(Node *head) {17    Node *slow = head, *fast = head;18    // fast 能走就走兩步,slow 走一步;相等就是有環19    return 0;20}21 22int main(void) {23    Node *c = new_node(3, NULL);24    Node *a = new_node(1, new_node(2, c));25    Node *x = new_node(7, new_node(8, new_node(9, NULL)));26    c->next = a->next;            // 3 指回 2:有環27    printf("%d %d\n", has_cycle(a), has_cycle(x));28    return 0;29}
輸出
點「執行」或按 Ctrl+Enter

合併兩個有序鏈結串列

兩個從小到大排好的鏈結串列,合成一個仍然有序的。套路是一個假的頭結點 + 一個 tail:每次比較兩邊的第一個,小的接到 tail 後面。

合併 a(1 4 6,綠)和 b(2 3 8,藍):每個結點落到下面合併後的位置,顏色說明它來自哪條;最前面的 dummy 是假的頭結點,返回的是 dummy.next
1Node dummy = {0, NULL};2Node *tail = &dummy;3while (a != NULL && b != NULL) {4    if (a->data <= b->data) { tail->next = a; a = a->next; }5    else                    { tail->next = b; b = b->next; }6    tail = tail->next;7}8// 剩下的整段接上9tail->next = (a != NULL) ? a : b;10return dummy.next;

用了假的頭結點,第一個結點就不用特殊處理(和「鏈結串列的插入和刪除」那一課裡的頭結點是同一個道理)。

填空點一個填進空格,再點空格可以拿回來

迴圈停下時,有一邊已經走完了。最後一行要把另一邊剩下的整段接上,空裡填哪個?程式要印出 1 2 3 4 6 8 9。

1#include <stdio.h>2#include <stdlib.h>3 4typedef struct Node {5    int data;6    struct Node *next;7} Node;8 9Node *new_node(int data, Node *next) {10    Node *n = malloc(sizeof(Node));11    n->data = data;12    n->next = next;13    return n;14}15 16Node *merge(Node *a, Node *b) {17    Node dummy = {0, NULL};18    Node *tail = &dummy;19    while (a != NULL && b != NULL) {20        if (a->data <= b->data) {21            tail->next = a;22            a = a->next;23        } else {24            tail->next = b;25            b = b->next;26        }27        tail = tail->next;28    }29    tail->next = ;30    return dummy.next;31}32 33int main(void) {34    Node *a = new_node(1, new_node(4, new_node(8, new_node(9, NULL))));35    Node *b = new_node(2, new_node(3, new_node(6, NULL)));36    for (Node *p = merge(a, b); p != NULL; p = p->next)37        printf("%d ", p->data);38    printf("\n");39    return 0;40}
練習 3

合併兩個有序鏈結串列

補全 merge:用假的頭結點和 tail,把兩個從小到大的鏈結串列合成一個。

要顯示
1 2 3 4 6 8 9
exercise3.c
1#include <stdio.h>2#include <stdlib.h>3 4typedef struct Node {5    int data;6    struct Node *next;7} Node;8 9Node *new_node(int data, Node *next) {10    Node *n = malloc(sizeof(Node));11    n->data = data;12    n->next = next;13    return n;14}15 16void show(Node *head) {17    for (Node *p = head; p != NULL; p = p->next)18        printf("%d ", p->data);19    printf("\n");20}21 22Node *merge(Node *a, Node *b) {23    Node dummy = {0, NULL};24    Node *tail = &dummy;25    // 兩邊都還有:小的接到 tail 後面;最後把剩下的整段接上26    return dummy.next;27}28 29int main(void) {30    Node *a = new_node(1, new_node(4, new_node(6, NULL)));31    Node *b = new_node(2, new_node(3, new_node(8, new_node(9, NULL))));32    show(merge(a, b));33    return 0;34}
輸出
點「執行」或按 Ctrl+Enter

小結

這一課帶走

鏈結串列五個套路

  • 選擇:按位置取用順序表,頻繁插刪用鏈結串列
  • 頭插法建出來是反的,尾插法要一個 tail
  • 逆置:prev、cur、next 三個指標
  • 快慢指標:找中間、判有環
  • 合併有序:假的頭結點 + tail
1next = cur->next; cur->next = prev; prev = cur; cur = next;2slow = slow->next; fast = fast->next->next;

第 1 章快刷

6 道題,覆蓋這一章的順序表、鏈結串列、動態陣列和最容易寫錯的那一步。先自己算出印出什麼,再點選項。

猜輸出猜錯也沒關係

長度 6 的順序表在下標 2 插入,挪了幾個?

1#include <stdio.h>2 3int main(void) {4    int a[8] = {1, 2, 3, 4, 5, 6};5    int len = 6, pos = 2, moves = 0;6    for (int i = len; i > pos; i--) {7        a[i] = a[i - 1];8        moves++;9    }10    a[pos] = 9;11    printf("%d\n", moves);12    return 0;13}
猜輸出猜錯也沒關係

p 是第二個結點,執行 p->next = p->next->next 以後印出什麼?

1#include <stdio.h>2#include <stdlib.h>3 4typedef struct Node {5    int data;6    struct Node *next;7} Node;8 9Node *new_node(int data, Node *next) {10    Node *n = malloc(sizeof(Node));11    n->data = data;12    n->next = next;13    return n;14}15 16int main(void) {17    Node *head = new_node(1, new_node(2, new_node(3, new_node(4, NULL))));18    Node *p = head->next;19    p->next = p->next->next;20    for (Node *q = head; q != NULL; q = q->next)21        printf("%d ", q->data);22    printf("\n");23    return 0;24}
猜輸出猜錯也沒關係

每次 head = new_node(x, head),印出什麼?

1#include <stdio.h>2#include <stdlib.h>3 4typedef struct Node {5    int data;6    struct Node *next;7} Node;8 9Node *new_node(int data, Node *next) {10    Node *n = malloc(sizeof(Node));11    n->data = data;12    n->next = next;13    return n;14}15 16int main(void) {17    Node *head = NULL;18    for (int x = 1; x <= 4; x++)19        head = new_node(x, head);20    for (Node *q = head; q != NULL; q = q->next)21        printf("%d ", q->data);22    printf("\n");23    return 0;24}
猜輸出猜錯也沒關係

1 到 6 共 6 個結點,快慢指標停下時 slow 指著幾?

1#include <stdio.h>2#include <stdlib.h>3 4typedef struct Node {5    int data;6    struct Node *next;7} Node;8 9Node *new_node(int data, Node *next) {10    Node *n = malloc(sizeof(Node));11    n->data = data;12    n->next = next;13    return n;14}15 16int main(void) {17    Node *head = NULL;18    for (int x = 6; x >= 1; x--)19        head = new_node(x, head);20    Node *slow = head, *fast = head;21    while (fast != NULL && fast->next != NULL) {22        slow = slow->next;23        fast = fast->next->next;24    }25    printf("%d\n", slow->data);26    return 0;27}
猜輸出猜錯也沒關係

從 1 格開始,滿了就翻倍,放進 9 個以後 len 和 cap 是多少?

1#include <stdio.h>2 3int main(void) {4    int cap = 1, len = 0;5    for (int x = 0; x < 9; x++) {6        if (len == cap)7            cap *= 2;8        len++;9    }10    printf("%d %d\n", len, cap);11    return 0;12}
猜輸出猜錯也沒關係

插入時兩步寫反了:先改 p->next,再接 s->next。印出前 4 個是什麼?

1#include <stdio.h>2#include <stdlib.h>3 4typedef struct Node {5    int data;6    struct Node *next;7} Node;8 9Node *new_node(int data, Node *next) {10    Node *n = malloc(sizeof(Node));11    n->data = data;12    n->next = next;13    return n;14}15 16int main(void) {17    Node *p = new_node(1, new_node(2, NULL));18    Node *s = new_node(5, NULL);19    p->next = s;20    s->next = p->next;21    Node *q = p;22    for (int k = 0; k < 4; k++, q = q->next)23        printf("%d ", q->data);24    printf("\n");25    return 0;26}