题型与套路:顺序表和链表
这一章的顺序表和链表总复习讲五类题:顺序表和链表怎么选、头插法和尾插法建表、用三个指针原地逆置、快慢指针找中间和判环、合并两个有序链表。
学完顺序表和链表,期末、考研 408 和面试最常考的就是这几类题,这一课一关讲一类。每一类记住一个套路,碰到变形也能套上去。
选择题:顺序表还是链表
先问自己:这份数据最常做的操作是什么?
| 最常做的 | 选 | 为什么 |
|---|---|---|
| 按位置取(第 i 个) | 顺序表 | O(1);链表要走 i 步 |
| 在已知结点前后插入、删除 | 链表 | O(1);顺序表要挪 |
| 事先不知道有多少个 | 链表(或动态数组) | 不用一次要一大块 |
| 很在意内存、要缓存友好 | 顺序表 | 不用存指针,连续内存读得快 |
常见陷阱: 「链表插入是 O(1)」前提是已经拿到前一个结点;「在第 i 个位置插入」两种都要 O(n):顺序表慢在挪,链表慢在找。
要存全班的成绩,最常做的是「查第 i 个同学的成绩」,几乎不插入、删除。选哪个?
a[i] 直接算出地址,O(1);链表要从头走 i 步,O(n)。几乎不插删,链表的长处也用不上。头插法和尾插法:建表
依次读进 1 2 3 4 5 建一个带头结点的链表,有两种插法:
- 头插法:每个新结点都插在头结点后面。建出来是反的:5 4 3 2 1。
- 尾插法:用一个
tail指针记着最后一个,每次接在它后面。顺序不变。
L↓L↓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 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}
tail 一直指着头结点,每次都是 L->next = 新结点,新来的把前一个顶掉了:最后只剩 3(1 和 2 还泄漏了)。尾插法每接一个,都要 tail = tail->next;。原地逆置
不另外要内存,把 1 → 2 → 3 改成 3 → 2 → 1。套路是三个指针:prev(已经反好的那段的头)、cur(正在处理的)、next(先记住下一个,免得断了找不到)。
cur↓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。
prev = cur;cur = next;cur->next = prev;Node *next = cur->next;
4 3 2 1
prev = cur; 要是放在 cur = next; 后面,prev 就跟错了结点。原地逆置
✓补全 reverse:用三个指针把链表原地逆过来,返回新的头。
4 3 2 1
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}
快慢指针:找中间、判有环
两个指针同时从头出发,slow 每次走 1 步,fast 每次走 2 步。fast 走到头时,slow 正好在中间。
slow↓fast↓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↓用快慢指针判断有没有环,时间和额外空间各是多少?
有没有环
✓补全 has_cycle:用快慢指针判断链表有没有环,有返回 1,没有返回 0。
1 0
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}
合并两个有序链表
两个从小到大排好的链表,合成一个仍然有序的。套路是一个假的头结点 + 一个 tail:每次比较两边的第一个,小的接到 tail 后面。
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}
b 或 NULL 都会把 8 9 丢掉。合并两个有序链表
✓补全 merge:用假的头结点和 tail,把两个从小到大的链表合成一个。
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 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}
小结
链表五个套路
- 选择:按位置取用顺序表,频繁插删用链表
- 头插法建出来是反的,尾插法要一个 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}