This commit is contained in:
krahets
2023-04-23 14:58:03 +08:00
parent 881ece517f
commit fe8027e64a
26 changed files with 344 additions and 626 deletions
+6 -12
View File
@@ -427,8 +427,7 @@ comments: true
```csharp title="linked_list.cs"
/* 在链表的节点 n0 之后插入节点 P */
void insert(ListNode n0, ListNode P)
{
void insert(ListNode n0, ListNode P) {
ListNode? n1 = n0.next;
P.next = n1;
n0.next = P;
@@ -570,8 +569,7 @@ comments: true
```csharp title="linked_list.cs"
/* 删除链表的节点 n0 之后的首个节点 */
void remove(ListNode n0)
{
void remove(ListNode n0) {
if (n0.next == null)
return;
// n0 -> P -> n1
@@ -716,10 +714,8 @@ comments: true
```csharp title="linked_list.cs"
/* 访问链表中索引为 index 的节点 */
ListNode? access(ListNode head, int index)
{
for (int i = 0; i < index; i++)
{
ListNode? access(ListNode head, int index) {
for (int i = 0; i < index; i++) {
if (head == null)
return null;
head = head.next;
@@ -882,11 +878,9 @@ comments: true
```csharp title="linked_list.cs"
/* 在链表中查找值为 target 的首个节点 */
int find(ListNode head, int target)
{
int find(ListNode head, int target) {
int index = 0;
while (head != null)
{
while (head != null) {
if (head.val == target)
return index;
head = head.next;