敬爱的李老师在期末才把线性表的题放出来,我说我怎么链表这么糊里糊涂就结束了。🍢🧆🥘🍲🍦🥧🍝
刚好我先去把线性表那一块的笔记复习一下。
# 1. 合并两个有序链表
行高亮 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 #include <iostream> using namespace std;struct Node { Node *next; int data; }; Node* InputList () { int input; Node *root = new Node; Node *node = root; while (true ) { cin >> input; if (input == -1 ) break ; Node* next = new Node; next->data = input; node->next = next; next->next = nullptr ; node = next; } return root; } Node* SortA_B (Node *A,Node*B) { Node *root = new Node; Node* node = root; A = A->next; B = B->next; while (A || B) { if (A && (!B || A->data <= B->data)) { Node* next = new Node; next->data = A->data; node->next = next; next->next = nullptr ; node = next; A = A->next; } else { Node* next = new Node; next->data = B->data; node->next = next; next->next = nullptr ; node = next; B = B->next; } } return root; } void print (Node* result) { result = result->next; while (result) { cout << result->data << " " ; result = result->next; } cout << endl; } int main () { Node* A = InputList (); Node* B = InputList (); Node* C = SortA_B (A, B); print (C); return 0 ; }
# 2. 删除排序链表中的重复元素
行高亮 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 #include <iostream> using namespace std;struct Node { Node* next; int data; }; Node* InputList () { int input; Node* root = new Node; Node* node = root; while (true ) { cin >> input; if (input == -1 ) break ; Node* next = new Node; next->data = input; node->next = next; next->next = nullptr ; node = next; } return root; } void init (Node* root) { root->data = -10000 ; root->next = nullptr ; } Node* DeleteRe (Node* Head) { Node* root = new Node; init (root); Node* node = root; Head = Head->next; while (Head) { if (Head->data != node->data) { Node* next = new Node; next->data =Head->data; node->next = next; next->next = nullptr ; node = next; } Head = Head->next; } return root; } void print (Node* result) { result = result->next; while (result) { cout << result->data << " " ; result = result->next; } cout << endl; } int main () { Node* input = InputList (); Node* Result = DeleteRe (input); print (Result); return 0 ; }
# 3. 移除链表元素
行高亮 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 #include <iostream> using namespace std;struct Node { Node* next; int data; }; void init (Node* root) { root->data = -10000 ; root->next = nullptr ; } Node* InputList () { int input; Node* root = new Node; init (root); Node* node = root; while (true ) { cin >> input; if (input == -1 ) break ; Node* next = new Node; next->data = input; node->next = next; next->next = nullptr ; node = next; } return root; } Node* Delete (Node* Head,int temp) { Node* root = Head; while (Head) { if (Head->next && Head->next->data == temp) { Node* op = Head->next; while (op && op->data == temp) { if (op->next) { op = op->next; } else op = nullptr ; } Head->next = op; } else Head=Head->next; } return root; } void print (Node* result) { result = result->next; while (result) { cout << result->data << " " ; result = result->next; } cout << endl; } int main () { Node* input = InputList (); int num; cin >> num; Node* Result = Delete (input,num); print (Result); return 0 ; }
# 4. 从尾到头打印链表(小疑问,前插法)
行高亮 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 #include <iostream> using namespace std;struct Node { Node* next; int data; }; void init (Node* root) { root->data = -10000 ; root->next = nullptr ; } Node* InputList () { int input; Node* root = new Node; init (root); Node* node = root; while (true ) { cin >> input; if (input == -1 ) break ; Node* next = new Node; next->data = input; node->next = next; next->next = nullptr ; node = next; } return root; } Node* ChangeTowards1111 (Node* input) { Node* root = new Node; init (root); input = input->next; while (input) { Node* op = input; op->next = root->next; root->next = op; input = input->next; } return root; } Node* ChangeTowards (Node* input) { Node* root = new Node; init (root); input = input->next; while (input) { Node* op = input; input = input->next; op->next = root->next; root->next = op; } return root; } void print (Node* result) { result = result->next; while (result) { cout << result->data << " " ; result = result->next; } cout << endl; } int main () { Node* input = InputList (); Node* Result = ChangeTowards (input); print (Result); return 0 ; }
# 5. 合并两个链表
行高亮 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 #include <iostream> using namespace std;struct Node { Node* next; int data; }; void init (Node* root) { root->data = -1000000 ; root->next = nullptr ; } Node* InputList () { int input; Node* root = new Node; init (root); Node* node = root; while (true ) { cin >> input; if (input == -1 ) break ; Node* next = new Node; next->data = input; node->next = next; next->next = nullptr ; node = next; } return root; } Node* DeleteA_AndB (Node* A, Node* B, int a, int b) { Node* root = A; int ans = 0 ; Node* Astart = new Node; Astart = B->next; Node* Aend = new Node; while (ans != b+2 ) { ans++; A = A->next; } Aend = A; while (B->next) { B = B->next; } B->next = Aend; Node* Result = root; for (int ans = 0 ; ans < a; ans++) { root = root->next; } root->next = Astart; return Result; } void print (Node* result) { result = result->next; while (result) { cout << result->data << " " ; result = result->next; } cout << endl; } int main () { Node* A = InputList (); int a, b; cin >> a >> b; Node* B = InputList (); Node* Result = DeleteA_AndB (A,B,a,b); print (Result); return 0 ; }
# 6. 二进制链表转整数 (同 4 题)
行高亮 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 #include <iostream> #include <math.h> using namespace std;struct Node { Node* next; int data; }; void init (Node* root) { root->data = -10000 ; root->next = nullptr ; } Node* InputList () { int input; Node* root = new Node; init (root); Node* node = root; while (true ) { cin >> input; if (input == -1 ) break ; Node* next = new Node; next->data = input; node->next = next; next->next = nullptr ; node = next; } return root; } Node* ChangeTowards (Node* input) { Node* root = new Node; init (root); input = input->next; while (input) { Node* op = input; input = input->next; op->next = root->next; root->next = op; } return root; } void Coal (Node* result) { result = result->next; int q = 0 ; int num = 0 ; while (result) { int p = pow (2 , q); num += result->data * p; q++; result = result->next; } cout << num<<endl; } int main () { Node* input = InputList (); Node* Result = ChangeTowards (input); Coal (Result); return 0 ; }
# 7. 两数相加 Ⅱ
行高亮 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 #include <iostream> #include <math.h> using namespace std;struct Node { Node* next; int data; }; void init (Node* root) { root->data = -10000 ; root->next = nullptr ; } Node* InputList () { int input; Node* root = new Node; init (root); Node* node = root; while (true ) { cin >> input; if (input == -1 ) break ; Node* next = new Node; next->data = input; node->next = next; next->next = nullptr ; node = next; } return root; } Node* ChangeTowards (Node* input) { Node* root = new Node; init (root); input = input->next; while (input) { Node* op = input; input = input->next; op->next = root->next; root->next = op; } return root; } int Coal (Node *A) { A = A->next; int q = 0 ; int num = 0 ; while (A) { int p = pow (10 , q); num += A->data * p; q++; A = A->next; } return num; } int And (Node* A, Node* B) { int num1 = Coal (A); int num2 = Coal (B); int result = num1 + num2; return result; } Node* NumCreatL (int num) { Node* root = new Node; init (root); int q = 0 ; int tmp = num / 10 ; int node = num - tmp * 10 ; while (num / 10 ) { int tmp = num / 10 ; int data = num - tmp * 10 ; Node* next = new Node; next->data = data; next->next = root->next; root->next = next; num /= 10 ; } Node* next = new Node; next->data = num; next->next = root->next; root->next = next; return root; } void print (Node* result) { result = result->next; while (result) { cout << result->data << " " ; result = result->next; } cout << endl; } int main () { Node* A = InputList (); Node* B = InputList (); Node* Result = NumCreatL (And (ChangeTowards (A), ChangeTowards (B))); print (Result); return 0 ; }
# 8. 反转链表 Ⅱ
行高亮 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 #include <iostream> using namespace std;struct Node { Node* next; int data; }; void init (Node* root) { root->data = -10000 ; root->next = nullptr ; } Node* InputList () { int input; Node* root = new Node; init (root); Node* node = root; while (true ) { cin >> input; if (input == -1 ) break ; Node* next = new Node; next->data = input; node->next = next; next->next = nullptr ; node = next; } return root; } void print (Node* result) { result = result->next; while (result) { cout << result->data << " " ; result = result->next; } cout << endl; } Node* ChangeLefttoRight (Node* L, int left, int right) { if (left == right) return L; Node* tempL = new Node (); init (tempL); Node* root = L; int ans = 0 ; Node* prev = nullptr ; Node* curr = L; while (curr) { if (ans >= left) { Node* next = curr->next; curr->next = tempL->next; tempL->next = curr; curr = next; } else { curr = curr->next; } if (ans == right) break ; ans++; } Node* L1 = tempL->next; while (L1->next) { L1 = L1->next; } L1->next = curr; Node* L2 = root; ans = 0 ; while (L2) { ans++; if (ans == left) { L2->next = tempL->next; break ; } L2 = L2->next; } return root; } int main () { Node* A = InputList (); int left, right; cin >> left >> right; Node* Result = ChangeLefttoRight (A, left, right); print (Result); return 0 ; }
# 9. 链表最大孪生和
行高亮 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 #include <iostream> #include <queue> using namespace std;struct Node { Node* next; int data; }; void init (Node* root) { root->data = -10000 ; root->next = nullptr ; } Node* InputList () { int input; Node* root = new Node; init (root); Node* node = root; while (true ) { cin >> input; if (input == -1 ) break ; Node* next = new Node; next->data = input; node->next = next; next->next = nullptr ; node = next; } return root; } Node* Copy (Node* L) { Node* L2 = new Node; init (L2); Node* curr = L->next; Node* prev = L2; while (curr) { Node* new_node = new Node; new_node->data = curr->data; new_node->next = nullptr ; prev->next = new_node; prev = new_node; curr = curr->next; } return L2; } Node* ChangeTowards (Node* L) { Node* input = L; Node* root = new Node; init (root); input = input->next; while (input) { Node* op = input; input = input->next; op->next = root->next; root->next = op; } return root; } void print (Node* result) { result = result->next; while (result) { cout << result->data << " " ; result = result->next; } cout << endl; } int Lenth (Node* L) { L = L->next; int lenth = 0 ; while (L) { lenth++; L = L->next; } return lenth; } int TwinnedMax (Node* L) { Node* L1 = L; Node* L2 = Copy (L1); L2 = ChangeTowards (L2); int lenth = Lenth (L2)/2 ; L1 = L1->next; L2 = L2->next; int ans = 0 ; int result = 0 ; while (ans++<lenth) { if (L2->data + L1->data > result) { result = L2->data + L1->data; } L1 = L1->next; L2 = L2->next; } return result; } int main () { Node* A = InputList (); int Result = TwinnedMax (A); cout << Result << endl; return 0 ; }
# 10. 和第 7 题重复
行高亮
# 11. 重排链表
行高亮 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 #include <iostream> using namespace std;struct Node { Node* next; int data; }; void init (Node* root) { root->data = -10000 ; root->next = nullptr ; } Node* InputList () { int input; Node* root = new Node; init (root); Node* node = root; while (true ) { cin >> input; if (input == -1 ) break ; Node* next = new Node; next->data = input; node->next = next; next->next = nullptr ; node = next; } return root; } Node* Copy (Node* L) { Node* L2 = new Node; init (L2); Node* curr = L->next; Node* prev = L2; while (curr) { Node* new_node = new Node; new_node->data = curr->data; new_node->next = nullptr ; prev->next = new_node; prev = new_node; curr = curr->next; } return L2; } Node* ChangeTowards (Node* L) { Node* input = L; Node* root = new Node; init (root); input = input->next; while (input) { Node* op = input; input = input->next; op->next = root->next; root->next = op; } return root; } void print (Node* result) { result = result->next; while (result) { cout << result->data << " " ; result = result->next; } cout << endl; } int Lenth (Node* L) { L = L->next; int lenth = 0 ; while (L) { lenth++; L = L->next; } return lenth; } Node* ChangeLtoN (Node* L) { Node* L1 = L; Node* L2 = Copy (L1); L2 = ChangeTowards (L2); int lenth = Lenth (L2); L1 = L1->next; L2 = L2->next; int ans = 0 ; Node* result = new Node; Node* node = result; while (ans++ < lenth) { if (ans % 2 == 1 ) { Node* next = new Node; next->data = L1->data; node->next = next; next->next = nullptr ; node = next; L1 = L1->next; } else { Node* next = new Node; next->data = L2->data; node->next = next; next->next = nullptr ; node = next; L2 = L2->next; } } return result; } int main () { Node* A = InputList (); Node* Result = ChangeLtoN (A); print (Result); return 0 ; }
# 12. 删除倒数第 n 个节点
行高亮 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 #include <iostream> using namespace std;struct Node { Node* next; int data; }; void init (Node* root) { root->data = -10000 ; root->next = nullptr ; } Node* InputList () { int input; Node* root = new Node; init (root); Node* node = root; while (true ) { cin >> input; if (input == -1 ) break ; Node* next = new Node; next->data = input; node->next = next; next->next = nullptr ; node = next; } return root; } void print (Node* result) { if (result == nullptr ) { cout << endl; return ; } result = result->next; while (result) { cout << result->data << " " ; result = result->next; } cout << endl; } int Lenth (Node* L) { L = L->next; int lenth = 0 ; while (L) { lenth++; L = L->next; } return lenth; } Node* Delete (Node* L, int n) { Node* root = L; Node* len = L; int lenth = Lenth (len); int num = lenth - n + 1 ; L = L->next; int ans = 0 ; while (++ans) { if (ans == num && num != lenth) { L->data = L->next->data; L->next = L->next->next; break ; } else if (ans == num - 1 && num == lenth) { L->next = nullptr ; break ; } else if (ans == num && num == 1 && lenth == 1 ) { return nullptr ; } else { L = L->next; } } return root; } int main () { Node* A = InputList (); int n; cin >> n; Node* Result = Delete (A,n); print (Result); return 0 ; }