This commit is contained in:
krahets
2023-04-14 04:01:38 +08:00
parent cf431646e9
commit 4d318e8e6b
26 changed files with 469 additions and 295 deletions
+7 -7
View File
@@ -363,8 +363,8 @@ comments: true
```cpp title="linked_list.cpp"
/* 在链表的节点 n0 之后插入节点 P */
void insert(ListNode* n0, ListNode* P) {
ListNode* n1 = n0->next;
void insert(ListNode *n0, ListNode *P) {
ListNode *n1 = n0->next;
P->next = n1;
n0->next = P;
}
@@ -477,12 +477,12 @@ comments: true
```cpp title="linked_list.cpp"
/* 删除链表的节点 n0 之后的首个节点 */
void remove(ListNode* n0) {
void remove(ListNode *n0) {
if (n0->next == nullptr)
return;
// n0 -> P -> n1
ListNode* P = n0->next;
ListNode* n1 = P->next;
ListNode *P = n0->next;
ListNode *n1 = P->next;
n0->next = n1;
// 释放内存
delete P;
@@ -618,7 +618,7 @@ comments: true
```cpp title="linked_list.cpp"
/* 访问链表中索引为 index 的节点 */
ListNode* access(ListNode* head, int index) {
ListNode *access(ListNode *head, int index) {
for (int i = 0; i < index; i++) {
if (head == nullptr)
return nullptr;
@@ -764,7 +764,7 @@ comments: true
```cpp title="linked_list.cpp"
/* 在链表中查找值为 target 的首个节点 */
int find(ListNode* head, int target) {
int find(ListNode *head, int target) {
int index = 0;
while (head != nullptr) {
if (head->val == target)