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}