mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-10 19:10:57 +00:00
build
This commit is contained in:
@@ -101,10 +101,9 @@ comments: true
|
||||
struct ListNode *next; // 指向下一节点的指针(引用)
|
||||
};
|
||||
|
||||
// typedef 作用是为一种数据类型定义一个新名字
|
||||
typedef struct ListNode ListNode;
|
||||
|
||||
/* 构造函数,初始化一个新节点 */
|
||||
/* 构造函数 */
|
||||
ListNode *newListNode(int val) {
|
||||
ListNode *node, *next;
|
||||
node = (ListNode *) malloc(sizeof(ListNode));
|
||||
@@ -416,7 +415,12 @@ comments: true
|
||||
=== "C"
|
||||
|
||||
```c title="linked_list.c"
|
||||
[class]{}-[func]{insertNode}
|
||||
/* 在链表的节点 n0 之后插入节点 P */
|
||||
void insert(ListNode *n0, ListNode *P) {
|
||||
ListNode *n1 = n0->next;
|
||||
P->next = n1;
|
||||
n0->next = P;
|
||||
}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
@@ -548,7 +552,18 @@ comments: true
|
||||
=== "C"
|
||||
|
||||
```c title="linked_list.c"
|
||||
[class]{}-[func]{removeNode}
|
||||
/* 删除链表的节点 n0 之后的首个节点 */
|
||||
// 注意:stdio.h 占用了 remove 关键词
|
||||
void removeNode(ListNode *n0) {
|
||||
if (!n0->next)
|
||||
return;
|
||||
// n0 -> P -> n1
|
||||
ListNode *P = n0->next;
|
||||
ListNode *n1 = P->next;
|
||||
n0->next = n1;
|
||||
// 释放内存
|
||||
free(P);
|
||||
}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
@@ -687,7 +702,14 @@ comments: true
|
||||
=== "C"
|
||||
|
||||
```c title="linked_list.c"
|
||||
[class]{}-[func]{access}
|
||||
/* 访问链表中索引为 index 的节点 */
|
||||
ListNode *access(ListNode *head, int index) {
|
||||
while (head && head->next && index) {
|
||||
head = head->next;
|
||||
index--;
|
||||
}
|
||||
return head;
|
||||
}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
@@ -843,7 +865,17 @@ comments: true
|
||||
=== "C"
|
||||
|
||||
```c title="linked_list.c"
|
||||
[class]{}-[func]{findNode}
|
||||
/* 在链表中查找值为 target 的首个节点 */
|
||||
int find(ListNode *head, int target) {
|
||||
int index = 0;
|
||||
while (head) {
|
||||
if (head->val == target)
|
||||
return index;
|
||||
head = head->next;
|
||||
index++;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
@@ -996,7 +1028,24 @@ comments: true
|
||||
=== "C"
|
||||
|
||||
```c title=""
|
||||
/* 双向链表节点结构体 */
|
||||
struct ListNode {
|
||||
int val; // 节点值
|
||||
struct ListNode *next; // 指向后继节点的指针(引用)
|
||||
struct ListNode *prev; // 指向前驱节点的指针(引用)
|
||||
};
|
||||
|
||||
typedef struct ListNode ListNode;
|
||||
|
||||
/* 构造函数 */
|
||||
ListNode *newListNode(int val) {
|
||||
ListNode *node, *next;
|
||||
node = (ListNode *) malloc(sizeof(ListNode));
|
||||
node->val = val;
|
||||
node->next = NULL;
|
||||
node->prev = NULL;
|
||||
return node;
|
||||
}
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
Reference in New Issue
Block a user