2023-02-08 15:20:18 +08:00
|
|
|
|
---
|
|
|
|
|
comments: true
|
|
|
|
|
---
|
|
|
|
|
|
2023-08-19 22:07:27 +08:00
|
|
|
|
# 4.2 链表
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-08-17 05:12:05 +08:00
|
|
|
|
内存空间是所有程序的公共资源,在一个复杂的系统运行环境下,空闲的内存空间可能散落在内存各处。我们知道,存储数组的内存空间必须是连续的,而当数组非常大时,内存可能无法提供如此大的连续空间。此时链表的灵活性优势就体现出来了。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2024-04-03 04:41:27 +08:00
|
|
|
|
<u>链表(linked list)</u>是一种线性数据结构,其中的每个元素都是一个节点对象,各个节点通过“引用”相连接。引用记录了下一个节点的内存地址,通过它可以从当前节点访问到下一个节点。
|
2023-08-21 19:32:37 +08:00
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
链表的设计使得各个节点可以分散存储在内存各处,它们的内存地址无须连续。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-11-09 05:13:48 +08:00
|
|
|
|
![链表定义与存储方式](linked_list.assets/linkedlist_definition.png){ class="animation-figure" }
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-08-22 13:50:12 +08:00
|
|
|
|
<p align="center"> 图 4-5 链表定义与存储方式 </p>
|
2023-08-17 05:12:05 +08:00
|
|
|
|
|
2024-04-03 04:41:27 +08:00
|
|
|
|
观察图 4-5 ,链表的组成单位是<u>节点(node)</u>对象。每个节点都包含两项数据:节点的“值”和指向下一节点的“引用”。
|
2023-08-17 05:12:05 +08:00
|
|
|
|
|
|
|
|
|
- 链表的首个节点被称为“头节点”,最后一个节点被称为“尾节点”。
|
2023-12-28 17:18:37 +08:00
|
|
|
|
- 尾节点指向的是“空”,它在 Java、C++ 和 Python 中分别被记为 `null`、`nullptr` 和 `None` 。
|
|
|
|
|
- 在 C、C++、Go 和 Rust 等支持指针的语言中,上述“引用”应被替换为“指针”。
|
2023-08-17 05:12:05 +08:00
|
|
|
|
|
2023-08-21 19:32:37 +08:00
|
|
|
|
如以下代码所示,链表节点 `ListNode` 除了包含值,还需额外保存一个引用(指针)。因此在相同数据量下,**链表比数组占用更多的内存空间**。
|
2023-02-26 19:53:26 +08:00
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
=== "Python"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
```python title=""
|
|
|
|
|
class ListNode:
|
|
|
|
|
"""链表节点类"""
|
|
|
|
|
def __init__(self, val: int):
|
2023-09-21 20:44:38 +08:00
|
|
|
|
self.val: int = val # 节点值
|
|
|
|
|
self.next: ListNode | None = None # 指向下一节点的引用
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title=""
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 链表节点结构体 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
struct ListNode {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
int val; // 节点值
|
2023-08-17 05:12:05 +08:00
|
|
|
|
ListNode *next; // 指向下一节点的指针
|
2023-02-08 15:20:18 +08:00
|
|
|
|
ListNode(int x) : val(x), next(nullptr) {} // 构造函数
|
|
|
|
|
};
|
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
=== "Java"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
```java title=""
|
|
|
|
|
/* 链表节点类 */
|
|
|
|
|
class ListNode {
|
|
|
|
|
int val; // 节点值
|
|
|
|
|
ListNode next; // 指向下一节点的引用
|
|
|
|
|
ListNode(int x) { val = x; } // 构造函数
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title=""
|
|
|
|
|
/* 链表节点类 */
|
2023-11-27 02:32:06 +08:00
|
|
|
|
class ListNode(int x) { //构造函数
|
|
|
|
|
int val = x; // 节点值
|
|
|
|
|
ListNode? next; // 指向下一节点的引用
|
2023-09-04 03:16:55 +08:00
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title=""
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 链表节点结构体 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
type ListNode struct {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
Val int // 节点值
|
2023-08-17 05:12:05 +08:00
|
|
|
|
Next *ListNode // 指向下一节点的指针
|
2023-02-08 15:20:18 +08:00
|
|
|
|
}
|
2023-08-17 05:12:05 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
// NewListNode 构造函数,创建一个新的链表
|
|
|
|
|
func NewListNode(val int) *ListNode {
|
|
|
|
|
return &ListNode{
|
|
|
|
|
Val: val,
|
|
|
|
|
Next: nil,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title=""
|
|
|
|
|
/* 链表节点类 */
|
|
|
|
|
class ListNode {
|
|
|
|
|
var val: Int // 节点值
|
|
|
|
|
var next: ListNode? // 指向下一节点的引用
|
|
|
|
|
|
|
|
|
|
init(x: Int) { // 构造函数
|
|
|
|
|
val = x
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:34:46 +08:00
|
|
|
|
=== "JS"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
```javascript title=""
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 链表节点类 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
class ListNode {
|
|
|
|
|
constructor(val, next) {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
this.val = (val === undefined ? 0 : val); // 节点值
|
|
|
|
|
this.next = (next === undefined ? null : next); // 指向下一节点的引用
|
2023-02-08 15:20:18 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:34:46 +08:00
|
|
|
|
=== "TS"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
```typescript title=""
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 链表节点类 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
class ListNode {
|
|
|
|
|
val: number;
|
|
|
|
|
next: ListNode | null;
|
|
|
|
|
constructor(val?: number, next?: ListNode | null) {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
this.val = val === undefined ? 0 : val; // 节点值
|
|
|
|
|
this.next = next === undefined ? null : next; // 指向下一节点的引用
|
2023-02-08 15:20:18 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
=== "Dart"
|
|
|
|
|
|
|
|
|
|
```dart title=""
|
|
|
|
|
/* 链表节点类 */
|
|
|
|
|
class ListNode {
|
|
|
|
|
int val; // 节点值
|
|
|
|
|
ListNode? next; // 指向下一节点的引用
|
|
|
|
|
ListNode(this.val, [this.next]); // 构造函数
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Rust"
|
|
|
|
|
|
|
|
|
|
```rust title=""
|
|
|
|
|
use std::rc::Rc;
|
|
|
|
|
use std::cell::RefCell;
|
|
|
|
|
/* 链表节点类 */
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
struct ListNode {
|
|
|
|
|
val: i32, // 节点值
|
|
|
|
|
next: Option<Rc<RefCell<ListNode>>>, // 指向下一节点的指针
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title=""
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 链表节点结构体 */
|
2023-10-18 02:16:55 +08:00
|
|
|
|
typedef struct ListNode {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
int val; // 节点值
|
2023-08-17 05:12:05 +08:00
|
|
|
|
struct ListNode *next; // 指向下一节点的指针
|
2023-10-18 02:16:55 +08:00
|
|
|
|
} ListNode;
|
2023-03-18 22:49:44 +08:00
|
|
|
|
|
2023-04-18 20:19:07 +08:00
|
|
|
|
/* 构造函数 */
|
2023-03-18 22:49:44 +08:00
|
|
|
|
ListNode *newListNode(int val) {
|
2023-11-14 21:31:21 +08:00
|
|
|
|
ListNode *node;
|
2023-03-18 22:49:44 +08:00
|
|
|
|
node = (ListNode *) malloc(sizeof(ListNode));
|
|
|
|
|
node->val = val;
|
|
|
|
|
node->next = NULL;
|
|
|
|
|
return node;
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
2024-03-25 22:43:12 +08:00
|
|
|
|
=== "Kotlin"
|
|
|
|
|
|
|
|
|
|
```kotlin title=""
|
2024-03-31 03:53:04 +08:00
|
|
|
|
/* 链表节点类 */
|
|
|
|
|
// 构造方法
|
|
|
|
|
class ListNode(x: Int) {
|
|
|
|
|
val _val: Int = x // 节点值
|
|
|
|
|
val next: ListNode? = null // 指向下一个节点的引用
|
|
|
|
|
}
|
|
|
|
|
```
|
2024-03-25 22:43:12 +08:00
|
|
|
|
|
2024-03-31 03:53:04 +08:00
|
|
|
|
=== "Ruby"
|
|
|
|
|
|
|
|
|
|
```ruby title=""
|
|
|
|
|
# 链表节点类
|
|
|
|
|
class ListNode
|
|
|
|
|
attr_accessor :val # 节点值
|
|
|
|
|
attr_accessor :next # 指向下一节点的引用
|
|
|
|
|
|
2024-04-01 05:25:50 +08:00
|
|
|
|
def initialize(val=0, next_node=nil)
|
|
|
|
|
@val = val
|
2024-03-31 03:53:04 +08:00
|
|
|
|
@next = next_node
|
|
|
|
|
end
|
|
|
|
|
end
|
2024-03-25 22:43:12 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title=""
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 链表节点类
|
2023-02-08 15:20:18 +08:00
|
|
|
|
pub fn ListNode(comptime T: type) type {
|
|
|
|
|
return struct {
|
|
|
|
|
const Self = @This();
|
2023-08-17 05:12:05 +08:00
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
val: T = 0, // 节点值
|
2023-08-17 05:12:05 +08:00
|
|
|
|
next: ?*Self = null, // 指向下一节点的指针
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
// 构造函数
|
|
|
|
|
pub fn init(self: *Self, x: i32) void {
|
|
|
|
|
self.val = x;
|
|
|
|
|
self.next = null;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
## 4.2.1 链表常用操作
|
2023-06-02 02:38:24 +08:00
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
### 1. 初始化链表
|
2023-06-02 02:38:24 +08:00
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
建立链表分为两步,第一步是初始化各个节点对象,第二步是构建节点之间的引用关系。初始化完成后,我们就可以从链表的头节点出发,通过引用指向 `next` 依次访问所有节点。
|
2023-07-26 10:57:40 +08:00
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
=== "Python"
|
2023-07-26 10:57:40 +08:00
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
```python title="linked_list.py"
|
|
|
|
|
# 初始化链表 1 -> 3 -> 2 -> 5 -> 4
|
|
|
|
|
# 初始化各个节点
|
|
|
|
|
n0 = ListNode(1)
|
|
|
|
|
n1 = ListNode(3)
|
|
|
|
|
n2 = ListNode(2)
|
|
|
|
|
n3 = ListNode(5)
|
|
|
|
|
n4 = ListNode(4)
|
2023-12-02 06:24:05 +08:00
|
|
|
|
# 构建节点之间的引用
|
2023-09-04 03:16:55 +08:00
|
|
|
|
n0.next = n1
|
|
|
|
|
n1.next = n2
|
|
|
|
|
n2.next = n3
|
|
|
|
|
n3.next = n4
|
|
|
|
|
```
|
2023-04-07 22:32:04 +08:00
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
=== "C++"
|
2023-08-17 05:12:05 +08:00
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
```cpp title="linked_list.cpp"
|
|
|
|
|
/* 初始化链表 1 -> 3 -> 2 -> 5 -> 4 */
|
|
|
|
|
// 初始化各个节点
|
|
|
|
|
ListNode* n0 = new ListNode(1);
|
|
|
|
|
ListNode* n1 = new ListNode(3);
|
|
|
|
|
ListNode* n2 = new ListNode(2);
|
|
|
|
|
ListNode* n3 = new ListNode(5);
|
|
|
|
|
ListNode* n4 = new ListNode(4);
|
2023-12-02 06:24:05 +08:00
|
|
|
|
// 构建节点之间的引用
|
2023-09-04 03:16:55 +08:00
|
|
|
|
n0->next = n1;
|
|
|
|
|
n1->next = n2;
|
|
|
|
|
n2->next = n3;
|
|
|
|
|
n3->next = n4;
|
|
|
|
|
```
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="linked_list.java"
|
|
|
|
|
/* 初始化链表 1 -> 3 -> 2 -> 5 -> 4 */
|
2023-08-17 05:12:05 +08:00
|
|
|
|
// 初始化各个节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
ListNode n0 = new ListNode(1);
|
|
|
|
|
ListNode n1 = new ListNode(3);
|
|
|
|
|
ListNode n2 = new ListNode(2);
|
|
|
|
|
ListNode n3 = new ListNode(5);
|
|
|
|
|
ListNode n4 = new ListNode(4);
|
2023-12-02 06:24:05 +08:00
|
|
|
|
// 构建节点之间的引用
|
2023-02-08 15:20:18 +08:00
|
|
|
|
n0.next = n1;
|
|
|
|
|
n1.next = n2;
|
|
|
|
|
n2.next = n3;
|
|
|
|
|
n3.next = n4;
|
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
=== "C#"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
```csharp title="linked_list.cs"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
/* 初始化链表 1 -> 3 -> 2 -> 5 -> 4 */
|
2023-08-17 05:12:05 +08:00
|
|
|
|
// 初始化各个节点
|
2023-10-08 01:43:28 +08:00
|
|
|
|
ListNode n0 = new(1);
|
|
|
|
|
ListNode n1 = new(3);
|
|
|
|
|
ListNode n2 = new(2);
|
|
|
|
|
ListNode n3 = new(5);
|
|
|
|
|
ListNode n4 = new(4);
|
2023-12-02 06:24:05 +08:00
|
|
|
|
// 构建节点之间的引用
|
2023-09-04 03:16:55 +08:00
|
|
|
|
n0.next = n1;
|
|
|
|
|
n1.next = n2;
|
|
|
|
|
n2.next = n3;
|
|
|
|
|
n3.next = n4;
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="linked_list.go"
|
|
|
|
|
/* 初始化链表 1 -> 3 -> 2 -> 5 -> 4 */
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 初始化各个节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
n0 := NewListNode(1)
|
|
|
|
|
n1 := NewListNode(3)
|
|
|
|
|
n2 := NewListNode(2)
|
|
|
|
|
n3 := NewListNode(5)
|
|
|
|
|
n4 := NewListNode(4)
|
2023-12-02 06:24:05 +08:00
|
|
|
|
// 构建节点之间的引用
|
2023-02-08 15:20:18 +08:00
|
|
|
|
n0.Next = n1
|
|
|
|
|
n1.Next = n2
|
|
|
|
|
n2.Next = n3
|
|
|
|
|
n3.Next = n4
|
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="linked_list.swift"
|
|
|
|
|
/* 初始化链表 1 -> 3 -> 2 -> 5 -> 4 */
|
|
|
|
|
// 初始化各个节点
|
|
|
|
|
let n0 = ListNode(x: 1)
|
|
|
|
|
let n1 = ListNode(x: 3)
|
|
|
|
|
let n2 = ListNode(x: 2)
|
|
|
|
|
let n3 = ListNode(x: 5)
|
|
|
|
|
let n4 = ListNode(x: 4)
|
2023-12-02 06:24:05 +08:00
|
|
|
|
// 构建节点之间的引用
|
2023-09-04 03:16:55 +08:00
|
|
|
|
n0.next = n1
|
|
|
|
|
n1.next = n2
|
|
|
|
|
n2.next = n3
|
|
|
|
|
n3.next = n4
|
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:34:46 +08:00
|
|
|
|
=== "JS"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
```javascript title="linked_list.js"
|
|
|
|
|
/* 初始化链表 1 -> 3 -> 2 -> 5 -> 4 */
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 初始化各个节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
const n0 = new ListNode(1);
|
|
|
|
|
const n1 = new ListNode(3);
|
|
|
|
|
const n2 = new ListNode(2);
|
|
|
|
|
const n3 = new ListNode(5);
|
|
|
|
|
const n4 = new ListNode(4);
|
2023-12-02 06:24:05 +08:00
|
|
|
|
// 构建节点之间的引用
|
2023-02-08 15:20:18 +08:00
|
|
|
|
n0.next = n1;
|
|
|
|
|
n1.next = n2;
|
|
|
|
|
n2.next = n3;
|
|
|
|
|
n3.next = n4;
|
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:34:46 +08:00
|
|
|
|
=== "TS"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
```typescript title="linked_list.ts"
|
|
|
|
|
/* 初始化链表 1 -> 3 -> 2 -> 5 -> 4 */
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 初始化各个节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
const n0 = new ListNode(1);
|
|
|
|
|
const n1 = new ListNode(3);
|
|
|
|
|
const n2 = new ListNode(2);
|
|
|
|
|
const n3 = new ListNode(5);
|
|
|
|
|
const n4 = new ListNode(4);
|
2023-12-02 06:24:05 +08:00
|
|
|
|
// 构建节点之间的引用
|
2023-02-08 15:20:18 +08:00
|
|
|
|
n0.next = n1;
|
|
|
|
|
n1.next = n2;
|
|
|
|
|
n2.next = n3;
|
|
|
|
|
n3.next = n4;
|
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
=== "Dart"
|
|
|
|
|
|
|
|
|
|
```dart title="linked_list.dart"
|
|
|
|
|
/* 初始化链表 1 -> 3 -> 2 -> 5 -> 4 */\
|
|
|
|
|
// 初始化各个节点
|
|
|
|
|
ListNode n0 = ListNode(1);
|
|
|
|
|
ListNode n1 = ListNode(3);
|
|
|
|
|
ListNode n2 = ListNode(2);
|
|
|
|
|
ListNode n3 = ListNode(5);
|
|
|
|
|
ListNode n4 = ListNode(4);
|
2023-12-02 06:24:05 +08:00
|
|
|
|
// 构建节点之间的引用
|
2023-09-04 03:16:55 +08:00
|
|
|
|
n0.next = n1;
|
|
|
|
|
n1.next = n2;
|
|
|
|
|
n2.next = n3;
|
|
|
|
|
n3.next = n4;
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Rust"
|
|
|
|
|
|
|
|
|
|
```rust title="linked_list.rs"
|
|
|
|
|
/* 初始化链表 1 -> 3 -> 2 -> 5 -> 4 */
|
|
|
|
|
// 初始化各个节点
|
|
|
|
|
let n0 = Rc::new(RefCell::new(ListNode { val: 1, next: None }));
|
|
|
|
|
let n1 = Rc::new(RefCell::new(ListNode { val: 3, next: None }));
|
|
|
|
|
let n2 = Rc::new(RefCell::new(ListNode { val: 2, next: None }));
|
|
|
|
|
let n3 = Rc::new(RefCell::new(ListNode { val: 5, next: None }));
|
|
|
|
|
let n4 = Rc::new(RefCell::new(ListNode { val: 4, next: None }));
|
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
// 构建节点之间的引用
|
2023-09-04 03:16:55 +08:00
|
|
|
|
n0.borrow_mut().next = Some(n1.clone());
|
|
|
|
|
n1.borrow_mut().next = Some(n2.clone());
|
|
|
|
|
n2.borrow_mut().next = Some(n3.clone());
|
|
|
|
|
n3.borrow_mut().next = Some(n4.clone());
|
|
|
|
|
```
|
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="linked_list.c"
|
2023-03-18 22:49:44 +08:00
|
|
|
|
/* 初始化链表 1 -> 3 -> 2 -> 5 -> 4 */
|
2023-08-17 05:12:05 +08:00
|
|
|
|
// 初始化各个节点
|
2023-03-18 22:49:44 +08:00
|
|
|
|
ListNode* n0 = newListNode(1);
|
|
|
|
|
ListNode* n1 = newListNode(3);
|
|
|
|
|
ListNode* n2 = newListNode(2);
|
|
|
|
|
ListNode* n3 = newListNode(5);
|
|
|
|
|
ListNode* n4 = newListNode(4);
|
2023-12-02 06:24:05 +08:00
|
|
|
|
// 构建节点之间的引用
|
2023-03-18 22:49:44 +08:00
|
|
|
|
n0->next = n1;
|
|
|
|
|
n1->next = n2;
|
|
|
|
|
n2->next = n3;
|
|
|
|
|
n3->next = n4;
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
2024-03-25 22:43:12 +08:00
|
|
|
|
=== "Kotlin"
|
|
|
|
|
|
|
|
|
|
```kotlin title="linked_list.kt"
|
2024-03-31 03:53:04 +08:00
|
|
|
|
/* 初始化链表 1 -> 3 -> 2 -> 5 -> 4 */
|
|
|
|
|
// 初始化各个节点
|
|
|
|
|
val n0 = ListNode(1)
|
|
|
|
|
val n1 = ListNode(3)
|
|
|
|
|
val n2 = ListNode(2)
|
|
|
|
|
val n3 = ListNode(5)
|
|
|
|
|
val n4 = ListNode(4)
|
|
|
|
|
// 构建节点之间的引用
|
|
|
|
|
n0.next = n1;
|
|
|
|
|
n1.next = n2;
|
|
|
|
|
n2.next = n3;
|
|
|
|
|
n3.next = n4;
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Ruby"
|
2024-03-25 22:43:12 +08:00
|
|
|
|
|
2024-03-31 03:53:04 +08:00
|
|
|
|
```ruby title="linked_list.rb"
|
|
|
|
|
# 初始化链表 1 -> 3 -> 2 -> 5 -> 4
|
|
|
|
|
# 初始化各个节点
|
2024-04-03 21:48:54 +08:00
|
|
|
|
n0 = ListNode.new(1)
|
|
|
|
|
n1 = ListNode.new(3)
|
|
|
|
|
n2 = ListNode.new(2)
|
|
|
|
|
n3 = ListNode.new(5)
|
|
|
|
|
n4 = ListNode.new(4)
|
2024-03-31 03:53:04 +08:00
|
|
|
|
# 构建节点之间的引用
|
|
|
|
|
n0.next = n1
|
|
|
|
|
n1.next = n2
|
|
|
|
|
n2.next = n3
|
|
|
|
|
n3.next = n4
|
2024-03-25 22:43:12 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
=== "Zig"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
```zig title="linked_list.zig"
|
|
|
|
|
// 初始化链表
|
2023-08-17 05:12:05 +08:00
|
|
|
|
// 初始化各个节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
var n0 = inc.ListNode(i32){.val = 1};
|
|
|
|
|
var n1 = inc.ListNode(i32){.val = 3};
|
|
|
|
|
var n2 = inc.ListNode(i32){.val = 2};
|
|
|
|
|
var n3 = inc.ListNode(i32){.val = 5};
|
|
|
|
|
var n4 = inc.ListNode(i32){.val = 4};
|
2023-12-02 06:24:05 +08:00
|
|
|
|
// 构建节点之间的引用
|
2023-02-08 15:20:18 +08:00
|
|
|
|
n0.next = &n1;
|
|
|
|
|
n1.next = &n2;
|
|
|
|
|
n2.next = &n3;
|
|
|
|
|
n3.next = &n4;
|
|
|
|
|
```
|
|
|
|
|
|
2024-01-07 03:26:23 +08:00
|
|
|
|
??? pythontutor "可视化运行"
|
|
|
|
|
|
2024-01-09 16:00:24 +08:00
|
|
|
|
<div style="height: 549px; width: 100%;"><iframe class="pythontutor-iframe" src="https://pythontutor.com/iframe-embed.html#code=class%20ListNode%3A%0A%20%20%20%20%22%22%22%E9%93%BE%E8%A1%A8%E8%8A%82%E7%82%B9%E7%B1%BB%22%22%22%0A%20%20%20%20def%20__init__%28self,%20val%3A%20int%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%23%20%E8%8A%82%E7%82%B9%E5%80%BC%0A%20%20%20%20%20%20%20%20self.next%3A%20ListNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%90%8E%E7%BB%A7%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E9%93%BE%E8%A1%A8%201%20-%3E%203%20-%3E%202%20-%3E%205%20-%3E%204%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%90%84%E4%B8%AA%E8%8A%82%E7%82%B9%0A%20%20%20%20n0%20%3D%20ListNode%281%29%0A%20%20%20%20n1%20%3D%20ListNode%283%29%0A%20%20%20%20n2%20%3D%20ListNode%282%29%0A%20%20%20%20n3%20%3D%20ListNode%285%29%0A%20%20%20%20n4%20%3D%20ListNode%284%29%0A%20%20%20%20%23%20%E6%9E%84%E5%BB%BA%E8%8A%82%E7%82%B9%E4%B9%8B%E9%97%B4%E7%9A%84%E5%BC%95%E7%94%A8%0A%20%20%20%20n0.next%20%3D%20n1%0A%20%20%20%20n1.next%20%3D%20n2%0A%20%20%20%20n2.next%20%3D%20n3%0A%20%20%20%20n3.next%20%3D%20n4&codeDivHeight=472&codeDivWidth=350&cumulative=false&curInstr=3&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false"> </iframe></div>
|
|
|
|
|
<div style="margin-top: 5px;"><a href="https://pythontutor.com/iframe-embed.html#code=class%20ListNode%3A%0A%20%20%20%20%22%22%22%E9%93%BE%E8%A1%A8%E8%8A%82%E7%82%B9%E7%B1%BB%22%22%22%0A%20%20%20%20def%20__init__%28self,%20val%3A%20int%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%23%20%E8%8A%82%E7%82%B9%E5%80%BC%0A%20%20%20%20%20%20%20%20self.next%3A%20ListNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%90%8E%E7%BB%A7%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E9%93%BE%E8%A1%A8%201%20-%3E%203%20-%3E%202%20-%3E%205%20-%3E%204%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%90%84%E4%B8%AA%E8%8A%82%E7%82%B9%0A%20%20%20%20n0%20%3D%20ListNode%281%29%0A%20%20%20%20n1%20%3D%20ListNode%283%29%0A%20%20%20%20n2%20%3D%20ListNode%282%29%0A%20%20%20%20n3%20%3D%20ListNode%285%29%0A%20%20%20%20n4%20%3D%20ListNode%284%29%0A%20%20%20%20%23%20%E6%9E%84%E5%BB%BA%E8%8A%82%E7%82%B9%E4%B9%8B%E9%97%B4%E7%9A%84%E5%BC%95%E7%94%A8%0A%20%20%20%20n0.next%20%3D%20n1%0A%20%20%20%20n1.next%20%3D%20n2%0A%20%20%20%20n2.next%20%3D%20n3%0A%20%20%20%20n3.next%20%3D%20n4&codeDivHeight=800&codeDivWidth=600&cumulative=false&curInstr=3&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false" target="_blank" rel="noopener noreferrer">全屏观看 ></a></div>
|
2024-01-07 03:26:23 +08:00
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
数组整体是一个变量,比如数组 `nums` 包含元素 `nums[0]` 和 `nums[1]` 等,而链表是由多个独立的节点对象组成的。**我们通常将头节点当作链表的代称**,比如以上代码中的链表可记作链表 `n0` 。
|
2023-08-06 23:19:07 +08:00
|
|
|
|
|
2023-08-20 13:37:08 +08:00
|
|
|
|
### 2. 插入节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
在链表中插入节点非常容易。如图 4-6 所示,假设我们想在相邻的两个节点 `n0` 和 `n1` 之间插入一个新节点 `P` ,**则只需改变两个节点引用(指针)即可**,时间复杂度为 $O(1)$ 。
|
2023-08-17 05:12:05 +08:00
|
|
|
|
|
|
|
|
|
相比之下,在数组中插入元素的时间复杂度为 $O(n)$ ,在大数据量下的效率较低。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-11-09 05:13:48 +08:00
|
|
|
|
![链表插入节点示例](linked_list.assets/linkedlist_insert_node.png){ class="animation-figure" }
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-08-22 13:50:12 +08:00
|
|
|
|
<p align="center"> 图 4-6 链表插入节点示例 </p>
|
2023-02-26 19:53:26 +08:00
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
=== "Python"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
```python title="linked_list.py"
|
2023-10-06 14:10:18 +08:00
|
|
|
|
def insert(n0: ListNode, P: ListNode):
|
|
|
|
|
"""在链表的节点 n0 之后插入节点 P"""
|
|
|
|
|
n1 = n0.next
|
|
|
|
|
P.next = n1
|
|
|
|
|
n0.next = P
|
2023-02-26 01:51:44 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="linked_list.cpp"
|
2023-10-06 14:10:18 +08:00
|
|
|
|
/* 在链表的节点 n0 之后插入节点 P */
|
|
|
|
|
void insert(ListNode *n0, ListNode *P) {
|
|
|
|
|
ListNode *n1 = n0->next;
|
|
|
|
|
P->next = n1;
|
|
|
|
|
n0->next = P;
|
|
|
|
|
}
|
2023-02-26 01:51:44 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
=== "Java"
|
2023-02-26 01:51:44 +08:00
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
```java title="linked_list.java"
|
2023-10-06 14:10:18 +08:00
|
|
|
|
/* 在链表的节点 n0 之后插入节点 P */
|
|
|
|
|
void insert(ListNode n0, ListNode P) {
|
|
|
|
|
ListNode n1 = n0.next;
|
|
|
|
|
P.next = n1;
|
|
|
|
|
n0.next = P;
|
|
|
|
|
}
|
2023-09-04 03:16:55 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="linked_list.cs"
|
2023-10-06 14:10:18 +08:00
|
|
|
|
/* 在链表的节点 n0 之后插入节点 P */
|
2023-10-08 01:43:28 +08:00
|
|
|
|
void Insert(ListNode n0, ListNode P) {
|
2023-10-06 14:10:18 +08:00
|
|
|
|
ListNode? n1 = n0.next;
|
|
|
|
|
P.next = n1;
|
|
|
|
|
n0.next = P;
|
|
|
|
|
}
|
2023-02-26 01:51:44 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="linked_list.go"
|
2023-10-06 14:10:18 +08:00
|
|
|
|
/* 在链表的节点 n0 之后插入节点 P */
|
|
|
|
|
func insertNode(n0 *ListNode, P *ListNode) {
|
|
|
|
|
n1 := n0.Next
|
|
|
|
|
P.Next = n1
|
|
|
|
|
n0.Next = P
|
|
|
|
|
}
|
2023-02-26 01:51:44 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="linked_list.swift"
|
2023-10-06 14:10:18 +08:00
|
|
|
|
/* 在链表的节点 n0 之后插入节点 P */
|
|
|
|
|
func insert(n0: ListNode, P: ListNode) {
|
|
|
|
|
let n1 = n0.next
|
|
|
|
|
P.next = n1
|
|
|
|
|
n0.next = P
|
|
|
|
|
}
|
2023-09-04 03:16:55 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:34:46 +08:00
|
|
|
|
=== "JS"
|
2023-02-26 01:51:44 +08:00
|
|
|
|
|
|
|
|
|
```javascript title="linked_list.js"
|
2023-10-06 14:10:18 +08:00
|
|
|
|
/* 在链表的节点 n0 之后插入节点 P */
|
|
|
|
|
function insert(n0, P) {
|
|
|
|
|
const n1 = n0.next;
|
|
|
|
|
P.next = n1;
|
|
|
|
|
n0.next = P;
|
|
|
|
|
}
|
2023-02-26 01:51:44 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:34:46 +08:00
|
|
|
|
=== "TS"
|
2023-02-26 01:51:44 +08:00
|
|
|
|
|
|
|
|
|
```typescript title="linked_list.ts"
|
2023-10-06 14:10:18 +08:00
|
|
|
|
/* 在链表的节点 n0 之后插入节点 P */
|
|
|
|
|
function insert(n0: ListNode, P: ListNode): void {
|
|
|
|
|
const n1 = n0.next;
|
|
|
|
|
P.next = n1;
|
|
|
|
|
n0.next = P;
|
|
|
|
|
}
|
2023-02-26 01:51:44 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
=== "Dart"
|
2023-02-26 01:51:44 +08:00
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
```dart title="linked_list.dart"
|
2023-10-06 14:10:18 +08:00
|
|
|
|
/* 在链表的节点 n0 之后插入节点 P */
|
|
|
|
|
void insert(ListNode n0, ListNode P) {
|
|
|
|
|
ListNode? n1 = n0.next;
|
|
|
|
|
P.next = n1;
|
|
|
|
|
n0.next = P;
|
|
|
|
|
}
|
2023-02-26 01:51:44 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
=== "Rust"
|
2023-02-26 01:51:44 +08:00
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
```rust title="linked_list.rs"
|
2023-10-06 14:10:18 +08:00
|
|
|
|
/* 在链表的节点 n0 之后插入节点 P */
|
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
|
pub fn insert<T>(n0: &Rc<RefCell<ListNode<T>>>, P: Rc<RefCell<ListNode<T>>>) {
|
2024-03-18 03:11:07 +08:00
|
|
|
|
let n1 = n0.borrow_mut().next.take();
|
2023-10-06 14:10:18 +08:00
|
|
|
|
P.borrow_mut().next = n1;
|
|
|
|
|
n0.borrow_mut().next = Some(P);
|
|
|
|
|
}
|
2023-02-26 01:51:44 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
=== "C"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
```c title="linked_list.c"
|
2023-10-06 14:10:18 +08:00
|
|
|
|
/* 在链表的节点 n0 之后插入节点 P */
|
|
|
|
|
void insert(ListNode *n0, ListNode *P) {
|
|
|
|
|
ListNode *n1 = n0->next;
|
|
|
|
|
P->next = n1;
|
|
|
|
|
n0->next = P;
|
|
|
|
|
}
|
2023-02-26 01:51:44 +08:00
|
|
|
|
```
|
|
|
|
|
|
2024-03-25 22:43:12 +08:00
|
|
|
|
=== "Kotlin"
|
|
|
|
|
|
|
|
|
|
```kotlin title="linked_list.kt"
|
|
|
|
|
/* 在链表的节点 n0 之后插入节点p */
|
|
|
|
|
fun insert(n0: ListNode?, p: ListNode?) {
|
|
|
|
|
val n1 = n0?.next
|
|
|
|
|
p?.next = n1
|
|
|
|
|
n0?.next = p
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2024-03-31 03:53:04 +08:00
|
|
|
|
=== "Ruby"
|
|
|
|
|
|
|
|
|
|
```ruby title="linked_list.rb"
|
|
|
|
|
### 在链表的节点 n0 之后插入节点 _p ###
|
|
|
|
|
# Ruby 的 `p` 是一个内置函数, `P` 是一个常量,所以可以使用 `_p` 代替
|
|
|
|
|
def insert(n0, _p)
|
|
|
|
|
n1 = n0.next
|
|
|
|
|
_p.next = n1
|
|
|
|
|
n0.next = _p
|
|
|
|
|
end
|
|
|
|
|
```
|
|
|
|
|
|
2023-02-26 01:51:44 +08:00
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="linked_list.zig"
|
2023-10-06 14:10:18 +08:00
|
|
|
|
// 在链表的节点 n0 之后插入节点 P
|
|
|
|
|
fn insert(n0: ?*inc.ListNode(i32), P: ?*inc.ListNode(i32)) void {
|
|
|
|
|
var n1 = n0.?.next;
|
|
|
|
|
P.?.next = n1;
|
|
|
|
|
n0.?.next = P;
|
|
|
|
|
}
|
2023-02-26 01:51:44 +08:00
|
|
|
|
```
|
|
|
|
|
|
2024-01-07 03:26:23 +08:00
|
|
|
|
??? pythontutor "可视化运行"
|
|
|
|
|
|
2024-01-09 16:00:24 +08:00
|
|
|
|
<div style="height: 549px; width: 100%;"><iframe class="pythontutor-iframe" src="https://pythontutor.com/iframe-embed.html#code=class%20ListNode%3A%0A%20%20%20%20%22%22%22%E9%93%BE%E8%A1%A8%E8%8A%82%E7%82%B9%E7%B1%BB%22%22%22%0A%20%20%20%20def%20__init__%28self,%20val%3A%20int%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%23%20%E8%8A%82%E7%82%B9%E5%80%BC%0A%20%20%20%20%20%20%20%20self.next%3A%20ListNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%90%8E%E7%BB%A7%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%0Adef%20insert%28n0%3A%20ListNode,%20P%3A%20ListNode%29%3A%0A%20%20%20%20%22%22%22%E5%9C%A8%E9%93%BE%E8%A1%A8%E7%9A%84%E8%8A%82%E7%82%B9%20n0%20%E4%B9%8B%E5%90%8E%E6%8F%92%E5%85%A5%E8%8A%82%E7%82%B9%20P%22%22%22%0A%20%20%20%20n1%20%3D%20n0.next%0A%20%20%20%20P.next%20%3D%20n1%0A%20%20%20%20n0.next%20%3D%20P%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E9%93%BE%E8%A1%A8%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%90%84%E4%B8%AA%E8%8A%82%E7%82%B9%0A%20%20%20%20n0%20%3D%20ListNode%281%29%0A%20%20%20%20n1%20%3D%20ListNode%283%29%0A%20%20%20%20n2%20%3D%20ListNode%282%29%0A%20%20%20%20n3%20%3D%20ListNode%285%29%0A%20%20%20%20n4%20%3D%20ListNode%284%29%0A%20%20%20%20%23%20%E6%9E%84%E5%BB%BA%E8%8A%82%E7%82%B9%E4%B9%8B%E9%97%B4%E7%9A%84%E5%BC%95%E7%94%A8%0A%20%20%20%20n0.next%20%3D%20n1%0A%20%20%20%20n1.next%20%3D%20n2%0A%20%20%20%20n2.next%20%3D%20n3%0A%20%20%20%20n3.next%20%3D%20n4%0A%0A%20%20%20%20%23%20%E6%8F%92%E5%85%A5%E8%8A%82%E7%82%B9%0A%20%20%20%20p%20%3D%20ListNode%280%29%0A%20%20%20%20insert%28n0,%20p%29&codeDivHeight=472&codeDivWidth=350&cumulative=false&curInstr=39&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false"> </iframe></div>
|
|
|
|
|
<div style="margin-top: 5px;"><a href="https://pythontutor.com/iframe-embed.html#code=class%20ListNode%3A%0A%20%20%20%20%22%22%22%E9%93%BE%E8%A1%A8%E8%8A%82%E7%82%B9%E7%B1%BB%22%22%22%0A%20%20%20%20def%20__init__%28self,%20val%3A%20int%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%23%20%E8%8A%82%E7%82%B9%E5%80%BC%0A%20%20%20%20%20%20%20%20self.next%3A%20ListNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%90%8E%E7%BB%A7%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%0Adef%20insert%28n0%3A%20ListNode,%20P%3A%20ListNode%29%3A%0A%20%20%20%20%22%22%22%E5%9C%A8%E9%93%BE%E8%A1%A8%E7%9A%84%E8%8A%82%E7%82%B9%20n0%20%E4%B9%8B%E5%90%8E%E6%8F%92%E5%85%A5%E8%8A%82%E7%82%B9%20P%22%22%22%0A%20%20%20%20n1%20%3D%20n0.next%0A%20%20%20%20P.next%20%3D%20n1%0A%20%20%20%20n0.next%20%3D%20P%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E9%93%BE%E8%A1%A8%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%90%84%E4%B8%AA%E8%8A%82%E7%82%B9%0A%20%20%20%20n0%20%3D%20ListNode%281%29%0A%20%20%20%20n1%20%3D%20ListNode%283%29%0A%20%20%20%20n2%20%3D%20ListNode%282%29%0A%20%20%20%20n3%20%3D%20ListNode%285%29%0A%20%20%20%20n4%20%3D%20ListNode%284%29%0A%20%20%20%20%23%20%E6%9E%84%E5%BB%BA%E8%8A%82%E7%82%B9%E4%B9%8B%E9%97%B4%E7%9A%84%E5%BC%95%E7%94%A8%0A%20%20%20%20n0.next%20%3D%20n1%0A%20%20%20%20n1.next%20%3D%20n2%0A%20%20%20%20n2.next%20%3D%20n3%0A%20%20%20%20n3.next%20%3D%20n4%0A%0A%20%20%20%20%23%20%E6%8F%92%E5%85%A5%E8%8A%82%E7%82%B9%0A%20%20%20%20p%20%3D%20ListNode%280%29%0A%20%20%20%20insert%28n0,%20p%29&codeDivHeight=800&codeDivWidth=600&cumulative=false&curInstr=39&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false" target="_blank" rel="noopener noreferrer">全屏观看 ></a></div>
|
2024-01-07 03:26:23 +08:00
|
|
|
|
|
2023-08-20 13:37:08 +08:00
|
|
|
|
### 3. 删除节点
|
2023-08-17 05:12:05 +08:00
|
|
|
|
|
2023-08-22 13:50:12 +08:00
|
|
|
|
如图 4-7 所示,在链表中删除节点也非常方便,**只需改变一个节点的引用(指针)即可**。
|
2023-08-17 05:12:05 +08:00
|
|
|
|
|
|
|
|
|
请注意,尽管在删除操作完成后节点 `P` 仍然指向 `n1` ,但实际上遍历此链表已经无法访问到 `P` ,这意味着 `P` 已经不再属于该链表了。
|
2023-02-26 01:51:44 +08:00
|
|
|
|
|
2023-11-09 05:13:48 +08:00
|
|
|
|
![链表删除节点](linked_list.assets/linkedlist_remove_node.png){ class="animation-figure" }
|
2023-02-26 01:51:44 +08:00
|
|
|
|
|
2023-08-22 13:50:12 +08:00
|
|
|
|
<p align="center"> 图 4-7 链表删除节点 </p>
|
2023-02-26 19:53:26 +08:00
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
=== "Python"
|
2023-02-26 01:51:44 +08:00
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
```python title="linked_list.py"
|
2023-10-06 14:10:18 +08:00
|
|
|
|
def remove(n0: ListNode):
|
|
|
|
|
"""删除链表的节点 n0 之后的首个节点"""
|
|
|
|
|
if not n0.next:
|
|
|
|
|
return
|
|
|
|
|
# n0 -> P -> n1
|
|
|
|
|
P = n0.next
|
|
|
|
|
n1 = P.next
|
|
|
|
|
n0.next = n1
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="linked_list.cpp"
|
2023-10-06 14:10:18 +08:00
|
|
|
|
/* 删除链表的节点 n0 之后的首个节点 */
|
|
|
|
|
void remove(ListNode *n0) {
|
|
|
|
|
if (n0->next == nullptr)
|
|
|
|
|
return;
|
|
|
|
|
// n0 -> P -> n1
|
|
|
|
|
ListNode *P = n0->next;
|
|
|
|
|
ListNode *n1 = P->next;
|
|
|
|
|
n0->next = n1;
|
|
|
|
|
// 释放内存
|
|
|
|
|
delete P;
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
=== "Java"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
```java title="linked_list.java"
|
2023-10-06 14:10:18 +08:00
|
|
|
|
/* 删除链表的节点 n0 之后的首个节点 */
|
|
|
|
|
void remove(ListNode n0) {
|
|
|
|
|
if (n0.next == null)
|
|
|
|
|
return;
|
|
|
|
|
// n0 -> P -> n1
|
|
|
|
|
ListNode P = n0.next;
|
|
|
|
|
ListNode n1 = P.next;
|
|
|
|
|
n0.next = n1;
|
|
|
|
|
}
|
2023-09-04 03:16:55 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="linked_list.cs"
|
2023-10-06 14:10:18 +08:00
|
|
|
|
/* 删除链表的节点 n0 之后的首个节点 */
|
2023-10-08 01:43:28 +08:00
|
|
|
|
void Remove(ListNode n0) {
|
2023-10-06 14:10:18 +08:00
|
|
|
|
if (n0.next == null)
|
|
|
|
|
return;
|
|
|
|
|
// n0 -> P -> n1
|
|
|
|
|
ListNode P = n0.next;
|
|
|
|
|
ListNode? n1 = P.next;
|
|
|
|
|
n0.next = n1;
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="linked_list.go"
|
2023-10-06 14:10:18 +08:00
|
|
|
|
/* 删除链表的节点 n0 之后的首个节点 */
|
2023-10-15 21:18:21 +08:00
|
|
|
|
func removeItem(n0 *ListNode) {
|
2023-10-06 14:10:18 +08:00
|
|
|
|
if n0.Next == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
// n0 -> P -> n1
|
|
|
|
|
P := n0.Next
|
|
|
|
|
n1 := P.Next
|
|
|
|
|
n0.Next = n1
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="linked_list.swift"
|
2023-10-06 14:10:18 +08:00
|
|
|
|
/* 删除链表的节点 n0 之后的首个节点 */
|
|
|
|
|
func remove(n0: ListNode) {
|
|
|
|
|
if n0.next == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
// n0 -> P -> n1
|
|
|
|
|
let P = n0.next
|
|
|
|
|
let n1 = P?.next
|
|
|
|
|
n0.next = n1
|
|
|
|
|
}
|
2023-09-04 03:16:55 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:34:46 +08:00
|
|
|
|
=== "JS"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
```javascript title="linked_list.js"
|
2023-10-06 14:10:18 +08:00
|
|
|
|
/* 删除链表的节点 n0 之后的首个节点 */
|
|
|
|
|
function remove(n0) {
|
|
|
|
|
if (!n0.next) return;
|
|
|
|
|
// n0 -> P -> n1
|
|
|
|
|
const P = n0.next;
|
|
|
|
|
const n1 = P.next;
|
|
|
|
|
n0.next = n1;
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:34:46 +08:00
|
|
|
|
=== "TS"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
```typescript title="linked_list.ts"
|
2023-10-06 14:10:18 +08:00
|
|
|
|
/* 删除链表的节点 n0 之后的首个节点 */
|
|
|
|
|
function remove(n0: ListNode): void {
|
|
|
|
|
if (!n0.next) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// n0 -> P -> n1
|
|
|
|
|
const P = n0.next;
|
|
|
|
|
const n1 = P.next;
|
|
|
|
|
n0.next = n1;
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
=== "Dart"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
```dart title="linked_list.dart"
|
2023-10-06 14:10:18 +08:00
|
|
|
|
/* 删除链表的节点 n0 之后的首个节点 */
|
|
|
|
|
void remove(ListNode n0) {
|
|
|
|
|
if (n0.next == null) return;
|
|
|
|
|
// n0 -> P -> n1
|
|
|
|
|
ListNode P = n0.next!;
|
|
|
|
|
ListNode? n1 = P.next;
|
|
|
|
|
n0.next = n1;
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
=== "Rust"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
```rust title="linked_list.rs"
|
2023-10-06 14:10:18 +08:00
|
|
|
|
/* 删除链表的节点 n0 之后的首个节点 */
|
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
|
pub fn remove<T>(n0: &Rc<RefCell<ListNode<T>>>) {
|
2024-03-18 03:11:07 +08:00
|
|
|
|
if n0.borrow().next.is_none() {
|
|
|
|
|
return;
|
|
|
|
|
};
|
2023-10-06 14:10:18 +08:00
|
|
|
|
// n0 -> P -> n1
|
|
|
|
|
let P = n0.borrow_mut().next.take();
|
|
|
|
|
if let Some(node) = P {
|
|
|
|
|
let n1 = node.borrow_mut().next.take();
|
|
|
|
|
n0.borrow_mut().next = n1;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
=== "C"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
```c title="linked_list.c"
|
2023-10-06 14:10:18 +08:00
|
|
|
|
/* 删除链表的节点 n0 之后的首个节点 */
|
|
|
|
|
// 注意:stdio.h 占用了 remove 关键词
|
2023-10-15 21:18:21 +08:00
|
|
|
|
void removeItem(ListNode *n0) {
|
2023-10-06 14:10:18 +08:00
|
|
|
|
if (!n0->next)
|
|
|
|
|
return;
|
|
|
|
|
// n0 -> P -> n1
|
|
|
|
|
ListNode *P = n0->next;
|
|
|
|
|
ListNode *n1 = P->next;
|
|
|
|
|
n0->next = n1;
|
|
|
|
|
// 释放内存
|
|
|
|
|
free(P);
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
2024-03-25 22:43:12 +08:00
|
|
|
|
=== "Kotlin"
|
|
|
|
|
|
|
|
|
|
```kotlin title="linked_list.kt"
|
|
|
|
|
/* 删除链表的节点 n0 之后的首个节点 */
|
|
|
|
|
fun remove(n0: ListNode?) {
|
|
|
|
|
val p = n0?.next
|
|
|
|
|
val n1 = p?.next
|
|
|
|
|
n0?.next = n1
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2024-03-31 03:53:04 +08:00
|
|
|
|
=== "Ruby"
|
|
|
|
|
|
|
|
|
|
```ruby title="linked_list.rb"
|
|
|
|
|
### 删除链表的节点 n0 之后的首个节点 ###
|
|
|
|
|
def remove(n0)
|
|
|
|
|
return if n0.next.nil?
|
|
|
|
|
|
|
|
|
|
# n0 -> remove_node -> n1
|
|
|
|
|
remove_node = n0.next
|
|
|
|
|
n1 = remove_node.next
|
|
|
|
|
n0.next = n1
|
|
|
|
|
end
|
|
|
|
|
```
|
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="linked_list.zig"
|
2023-10-06 14:10:18 +08:00
|
|
|
|
// 删除链表的节点 n0 之后的首个节点
|
|
|
|
|
fn remove(n0: ?*inc.ListNode(i32)) void {
|
|
|
|
|
if (n0.?.next == null) return;
|
|
|
|
|
// n0 -> P -> n1
|
|
|
|
|
var P = n0.?.next;
|
|
|
|
|
var n1 = P.?.next;
|
|
|
|
|
n0.?.next = n1;
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
2024-01-07 03:26:23 +08:00
|
|
|
|
??? pythontutor "可视化运行"
|
|
|
|
|
|
2024-01-09 16:00:24 +08:00
|
|
|
|
<div style="height: 549px; width: 100%;"><iframe class="pythontutor-iframe" src="https://pythontutor.com/iframe-embed.html#code=class%20ListNode%3A%0A%20%20%20%20%22%22%22%E9%93%BE%E8%A1%A8%E8%8A%82%E7%82%B9%E7%B1%BB%22%22%22%0A%20%20%20%20def%20__init__%28self,%20val%3A%20int%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%23%20%E8%8A%82%E7%82%B9%E5%80%BC%0A%20%20%20%20%20%20%20%20self.next%3A%20ListNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%90%8E%E7%BB%A7%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%0Adef%20remove%28n0%3A%20ListNode%29%3A%0A%20%20%20%20%22%22%22%E5%88%A0%E9%99%A4%E9%93%BE%E8%A1%A8%E7%9A%84%E8%8A%82%E7%82%B9%20n0%20%E4%B9%8B%E5%90%8E%E7%9A%84%E9%A6%96%E4%B8%AA%E8%8A%82%E7%82%B9%22%22%22%0A%20%20%20%20if%20not%20n0.next%3A%0A%20%20%20%20%20%20%20%20return%0A%20%20%20%20%23%20n0%20-%3E%20P%20-%3E%20n1%0A%20%20%20%20P%20%3D%20n0.next%0A%20%20%20%20n1%20%3D%20P.next%0A%20%20%20%20n0.next%20%3D%20n1%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E9%93%BE%E8%A1%A8%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%90%84%E4%B8%AA%E8%8A%82%E7%82%B9%0A%20%20%20%20n0%20%3D%20ListNode%281%29%0A%20%20%20%20n1%20%3D%20ListNode%283%29%0A%20%20%20%20n2%20%3D%20ListNode%282%29%0A%20%20%20%20n3%20%3D%20ListNode%285%29%0A%20%20%20%20n4%20%3D%20ListNode%284%29%0A%20%20%20%20%23%20%E6%9E%84%E5%BB%BA%E8%8A%82%E7%82%B9%E4%B9%8B%E9%97%B4%E7%9A%84%E5%BC%95%E7%94%A8%0A%20%20%20%20n0.next%20%3D%20n1%0A%20%20%20%20n1.next%20%3D%20n2%0A%20%20%20%20n2.next%20%3D%20n3%0A%20%20%20%20n3.next%20%3D%20n4%0A%0A%20%20%20%20%23%20%E5%88%A0%E9%99%A4%E8%8A%82%E7%82%B9%0A%20%20%20%20remove%28n0%29&codeDivHeight=472&codeDivWidth=350&cumulative=false&curInstr=34&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false"> </iframe></div>
|
|
|
|
|
<div style="margin-top: 5px;"><a href="https://pythontutor.com/iframe-embed.html#code=class%20ListNode%3A%0A%20%20%20%20%22%22%22%E9%93%BE%E8%A1%A8%E8%8A%82%E7%82%B9%E7%B1%BB%22%22%22%0A%20%20%20%20def%20__init__%28self,%20val%3A%20int%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%23%20%E8%8A%82%E7%82%B9%E5%80%BC%0A%20%20%20%20%20%20%20%20self.next%3A%20ListNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%90%8E%E7%BB%A7%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%0Adef%20remove%28n0%3A%20ListNode%29%3A%0A%20%20%20%20%22%22%22%E5%88%A0%E9%99%A4%E9%93%BE%E8%A1%A8%E7%9A%84%E8%8A%82%E7%82%B9%20n0%20%E4%B9%8B%E5%90%8E%E7%9A%84%E9%A6%96%E4%B8%AA%E8%8A%82%E7%82%B9%22%22%22%0A%20%20%20%20if%20not%20n0.next%3A%0A%20%20%20%20%20%20%20%20return%0A%20%20%20%20%23%20n0%20-%3E%20P%20-%3E%20n1%0A%20%20%20%20P%20%3D%20n0.next%0A%20%20%20%20n1%20%3D%20P.next%0A%20%20%20%20n0.next%20%3D%20n1%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E9%93%BE%E8%A1%A8%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%90%84%E4%B8%AA%E8%8A%82%E7%82%B9%0A%20%20%20%20n0%20%3D%20ListNode%281%29%0A%20%20%20%20n1%20%3D%20ListNode%283%29%0A%20%20%20%20n2%20%3D%20ListNode%282%29%0A%20%20%20%20n3%20%3D%20ListNode%285%29%0A%20%20%20%20n4%20%3D%20ListNode%284%29%0A%20%20%20%20%23%20%E6%9E%84%E5%BB%BA%E8%8A%82%E7%82%B9%E4%B9%8B%E9%97%B4%E7%9A%84%E5%BC%95%E7%94%A8%0A%20%20%20%20n0.next%20%3D%20n1%0A%20%20%20%20n1.next%20%3D%20n2%0A%20%20%20%20n2.next%20%3D%20n3%0A%20%20%20%20n3.next%20%3D%20n4%0A%0A%20%20%20%20%23%20%E5%88%A0%E9%99%A4%E8%8A%82%E7%82%B9%0A%20%20%20%20remove%28n0%29&codeDivHeight=800&codeDivWidth=600&cumulative=false&curInstr=34&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false" target="_blank" rel="noopener noreferrer">全屏观看 ></a></div>
|
2024-01-07 03:26:23 +08:00
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
### 4. 访问节点
|
2023-06-02 02:38:24 +08:00
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
**在链表中访问节点的效率较低**。如上一节所述,我们可以在 $O(1)$ 时间下访问数组中的任意元素。链表则不然,程序需要从头节点出发,逐个向后遍历,直至找到目标节点。也就是说,访问链表的第 $i$ 个节点需要循环 $i - 1$ 轮,时间复杂度为 $O(n)$ 。
|
2023-09-04 03:16:55 +08:00
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="linked_list.py"
|
2023-10-06 14:10:18 +08:00
|
|
|
|
def access(head: ListNode, index: int) -> ListNode | None:
|
|
|
|
|
"""访问链表中索引为 index 的节点"""
|
|
|
|
|
for _ in range(index):
|
|
|
|
|
if not head:
|
|
|
|
|
return None
|
|
|
|
|
head = head.next
|
|
|
|
|
return head
|
2023-06-02 02:38:24 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
=== "C++"
|
2023-07-26 10:57:40 +08:00
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
```cpp title="linked_list.cpp"
|
2023-10-06 14:10:18 +08:00
|
|
|
|
/* 访问链表中索引为 index 的节点 */
|
|
|
|
|
ListNode *access(ListNode *head, int index) {
|
|
|
|
|
for (int i = 0; i < index; i++) {
|
|
|
|
|
if (head == nullptr)
|
|
|
|
|
return nullptr;
|
|
|
|
|
head = head->next;
|
|
|
|
|
}
|
|
|
|
|
return head;
|
|
|
|
|
}
|
2023-07-26 10:57:40 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="linked_list.java"
|
2023-10-06 14:10:18 +08:00
|
|
|
|
/* 访问链表中索引为 index 的节点 */
|
|
|
|
|
ListNode access(ListNode head, int index) {
|
|
|
|
|
for (int i = 0; i < index; i++) {
|
|
|
|
|
if (head == null)
|
|
|
|
|
return null;
|
|
|
|
|
head = head.next;
|
|
|
|
|
}
|
|
|
|
|
return head;
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
=== "C#"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
```csharp title="linked_list.cs"
|
2023-10-06 14:10:18 +08:00
|
|
|
|
/* 访问链表中索引为 index 的节点 */
|
2023-11-27 02:32:06 +08:00
|
|
|
|
ListNode? Access(ListNode? head, int index) {
|
2023-10-06 14:10:18 +08:00
|
|
|
|
for (int i = 0; i < index; i++) {
|
|
|
|
|
if (head == null)
|
|
|
|
|
return null;
|
|
|
|
|
head = head.next;
|
|
|
|
|
}
|
|
|
|
|
return head;
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="linked_list.go"
|
2023-10-06 14:10:18 +08:00
|
|
|
|
/* 访问链表中索引为 index 的节点 */
|
|
|
|
|
func access(head *ListNode, index int) *ListNode {
|
|
|
|
|
for i := 0; i < index; i++ {
|
|
|
|
|
if head == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
head = head.Next
|
|
|
|
|
}
|
|
|
|
|
return head
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="linked_list.swift"
|
2023-10-06 14:10:18 +08:00
|
|
|
|
/* 访问链表中索引为 index 的节点 */
|
|
|
|
|
func access(head: ListNode, index: Int) -> ListNode? {
|
|
|
|
|
var head: ListNode? = head
|
|
|
|
|
for _ in 0 ..< index {
|
|
|
|
|
if head == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
head = head?.next
|
|
|
|
|
}
|
|
|
|
|
return head
|
|
|
|
|
}
|
2023-09-04 03:16:55 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:34:46 +08:00
|
|
|
|
=== "JS"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
```javascript title="linked_list.js"
|
2023-10-06 14:10:18 +08:00
|
|
|
|
/* 访问链表中索引为 index 的节点 */
|
|
|
|
|
function access(head, index) {
|
|
|
|
|
for (let i = 0; i < index; i++) {
|
|
|
|
|
if (!head) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
head = head.next;
|
|
|
|
|
}
|
|
|
|
|
return head;
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:34:46 +08:00
|
|
|
|
=== "TS"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
```typescript title="linked_list.ts"
|
2023-10-06 14:10:18 +08:00
|
|
|
|
/* 访问链表中索引为 index 的节点 */
|
|
|
|
|
function access(head: ListNode | null, index: number): ListNode | null {
|
|
|
|
|
for (let i = 0; i < index; i++) {
|
|
|
|
|
if (!head) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
head = head.next;
|
|
|
|
|
}
|
|
|
|
|
return head;
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
=== "Dart"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
```dart title="linked_list.dart"
|
2023-10-06 14:10:18 +08:00
|
|
|
|
/* 访问链表中索引为 index 的节点 */
|
|
|
|
|
ListNode? access(ListNode? head, int index) {
|
|
|
|
|
for (var i = 0; i < index; i++) {
|
|
|
|
|
if (head == null) return null;
|
|
|
|
|
head = head.next;
|
|
|
|
|
}
|
|
|
|
|
return head;
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
=== "Rust"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
```rust title="linked_list.rs"
|
2023-10-06 14:10:18 +08:00
|
|
|
|
/* 访问链表中索引为 index 的节点 */
|
|
|
|
|
pub fn access<T>(head: Rc<RefCell<ListNode<T>>>, index: i32) -> Rc<RefCell<ListNode<T>>> {
|
2024-03-18 03:11:07 +08:00
|
|
|
|
if index <= 0 {
|
|
|
|
|
return head;
|
|
|
|
|
};
|
|
|
|
|
if let Some(node) = &head.borrow().next {
|
2023-10-06 14:10:18 +08:00
|
|
|
|
return access(node.clone(), index - 1);
|
|
|
|
|
}
|
2024-03-18 03:11:07 +08:00
|
|
|
|
|
2023-10-06 14:10:18 +08:00
|
|
|
|
return head;
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
=== "C"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
```c title="linked_list.c"
|
2023-10-06 14:10:18 +08:00
|
|
|
|
/* 访问链表中索引为 index 的节点 */
|
|
|
|
|
ListNode *access(ListNode *head, int index) {
|
2023-12-22 02:57:23 +08:00
|
|
|
|
for (int i = 0; i < index; i++) {
|
|
|
|
|
if (head == NULL)
|
|
|
|
|
return NULL;
|
2023-10-06 14:10:18 +08:00
|
|
|
|
head = head->next;
|
|
|
|
|
}
|
|
|
|
|
return head;
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
2024-03-25 22:43:12 +08:00
|
|
|
|
=== "Kotlin"
|
|
|
|
|
|
|
|
|
|
```kotlin title="linked_list.kt"
|
|
|
|
|
/* 访问链表中索引为 index 的节点 */
|
|
|
|
|
fun access(head: ListNode?, index: Int): ListNode? {
|
|
|
|
|
var h = head
|
|
|
|
|
for (i in 0..<index) {
|
|
|
|
|
h = h?.next
|
|
|
|
|
}
|
|
|
|
|
return h
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2024-03-31 03:53:04 +08:00
|
|
|
|
=== "Ruby"
|
|
|
|
|
|
|
|
|
|
```ruby title="linked_list.rb"
|
|
|
|
|
### 访问链表中索引为 index 的节点 ###
|
|
|
|
|
def access(head, index)
|
|
|
|
|
for i in 0...index
|
|
|
|
|
return nil if head.nil?
|
|
|
|
|
head = head.next
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
head
|
|
|
|
|
end
|
|
|
|
|
```
|
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="linked_list.zig"
|
2023-10-06 14:10:18 +08:00
|
|
|
|
// 访问链表中索引为 index 的节点
|
|
|
|
|
fn access(node: ?*inc.ListNode(i32), index: i32) ?*inc.ListNode(i32) {
|
|
|
|
|
var head = node;
|
|
|
|
|
var i: i32 = 0;
|
|
|
|
|
while (i < index) : (i += 1) {
|
|
|
|
|
head = head.?.next;
|
|
|
|
|
if (head == null) return null;
|
|
|
|
|
}
|
|
|
|
|
return head;
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
2024-01-07 03:26:23 +08:00
|
|
|
|
??? pythontutor "可视化运行"
|
|
|
|
|
|
2024-01-09 16:00:24 +08:00
|
|
|
|
<div style="height: 549px; width: 100%;"><iframe class="pythontutor-iframe" src="https://pythontutor.com/iframe-embed.html#code=class%20ListNode%3A%0A%20%20%20%20%22%22%22%E9%93%BE%E8%A1%A8%E8%8A%82%E7%82%B9%E7%B1%BB%22%22%22%0A%20%20%20%20def%20__init__%28self,%20val%3A%20int%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%23%20%E8%8A%82%E7%82%B9%E5%80%BC%0A%20%20%20%20%20%20%20%20self.next%3A%20ListNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%90%8E%E7%BB%A7%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%0Adef%20access%28head%3A%20ListNode,%20index%3A%20int%29%20-%3E%20ListNode%20%7C%20None%3A%0A%20%20%20%20%22%22%22%E8%AE%BF%E9%97%AE%E9%93%BE%E8%A1%A8%E4%B8%AD%E7%B4%A2%E5%BC%95%E4%B8%BA%20index%20%E7%9A%84%E8%8A%82%E7%82%B9%22%22%22%0A%20%20%20%20for%20_%20in%20range%28index%29%3A%0A%20%20%20%20%20%20%20%20if%20not%20head%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20None%0A%20%20%20%20%20%20%20%20head%20%3D%20head.next%0A%20%20%20%20return%20head%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E9%93%BE%E8%A1%A8%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%90%84%E4%B8%AA%E8%8A%82%E7%82%B9%0A%20%20%20%20n0%20%3D%20ListNode%281%29%0A%20%20%20%20n1%20%3D%20ListNode%283%29%0A%20%20%20%20n2%20%3D%20ListNode%282%29%0A%20%20%20%20n3%20%3D%20ListNode%285%29%0A%20%20%20%20n4%20%3D%20ListNode%284%29%0A%20%20%20%20%23%20%E6%9E%84%E5%BB%BA%E8%8A%82%E7%82%B9%E4%B9%8B%E9%97%B4%E7%9A%84%E5%BC%95%E7%94%A8%0A%20%20%20%20n0.next%20%3D%20n1%0A%20%20%20%20n1.next%20%3D%20n2%0A%20%20%20%20n2.next%20%3D%20n3%0A%20%20%20%20n3.next%20%3D%20n4%0A%0A%20%20%20%20%23%20%E8%AE%BF%E9%97%AE%E8%8A%82%E7%82%B9%0A%20%20%20%20node%20%3D%20access%28n0,%203%29%0A%20%20%20%20print%28%22%E9%93%BE%E8%A1%A8%E4%B8%AD%E7%B4%A2%E5%BC%95%203%20%E5%A4%84%E7%9A%84%E8%8A%82%E7%82%B9%E7%9A%84%E5%80%BC%20%3D%20%7B%7D%22.format%28node.val%29%29&codeDivHeight=472&codeDivWidth=350&cumulative=false&curInstr=34&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false"> </iframe></div>
|
|
|
|
|
<div style="margin-top: 5px;"><a href="https://pythontutor.com/iframe-embed.html#code=class%20ListNode%3A%0A%20%20%20%20%22%22%22%E9%93%BE%E8%A1%A8%E8%8A%82%E7%82%B9%E7%B1%BB%22%22%22%0A%20%20%20%20def%20__init__%28self,%20val%3A%20int%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%23%20%E8%8A%82%E7%82%B9%E5%80%BC%0A%20%20%20%20%20%20%20%20self.next%3A%20ListNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%90%8E%E7%BB%A7%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%0Adef%20access%28head%3A%20ListNode,%20index%3A%20int%29%20-%3E%20ListNode%20%7C%20None%3A%0A%20%20%20%20%22%22%22%E8%AE%BF%E9%97%AE%E9%93%BE%E8%A1%A8%E4%B8%AD%E7%B4%A2%E5%BC%95%E4%B8%BA%20index%20%E7%9A%84%E8%8A%82%E7%82%B9%22%22%22%0A%20%20%20%20for%20_%20in%20range%28index%29%3A%0A%20%20%20%20%20%20%20%20if%20not%20head%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20None%0A%20%20%20%20%20%20%20%20head%20%3D%20head.next%0A%20%20%20%20return%20head%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E9%93%BE%E8%A1%A8%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%90%84%E4%B8%AA%E8%8A%82%E7%82%B9%0A%20%20%20%20n0%20%3D%20ListNode%281%29%0A%20%20%20%20n1%20%3D%20ListNode%283%29%0A%20%20%20%20n2%20%3D%20ListNode%282%29%0A%20%20%20%20n3%20%3D%20ListNode%285%29%0A%20%20%20%20n4%20%3D%20ListNode%284%29%0A%20%20%20%20%23%20%E6%9E%84%E5%BB%BA%E8%8A%82%E7%82%B9%E4%B9%8B%E9%97%B4%E7%9A%84%E5%BC%95%E7%94%A8%0A%20%20%20%20n0.next%20%3D%20n1%0A%20%20%20%20n1.next%20%3D%20n2%0A%20%20%20%20n2.next%20%3D%20n3%0A%20%20%20%20n3.next%20%3D%20n4%0A%0A%20%20%20%20%23%20%E8%AE%BF%E9%97%AE%E8%8A%82%E7%82%B9%0A%20%20%20%20node%20%3D%20access%28n0,%203%29%0A%20%20%20%20print%28%22%E9%93%BE%E8%A1%A8%E4%B8%AD%E7%B4%A2%E5%BC%95%203%20%E5%A4%84%E7%9A%84%E8%8A%82%E7%82%B9%E7%9A%84%E5%80%BC%20%3D%20%7B%7D%22.format%28node.val%29%29&codeDivHeight=800&codeDivWidth=600&cumulative=false&curInstr=34&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false" target="_blank" rel="noopener noreferrer">全屏观看 ></a></div>
|
2024-01-07 03:26:23 +08:00
|
|
|
|
|
2023-08-20 13:37:08 +08:00
|
|
|
|
### 5. 查找节点
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
遍历链表,查找其中值为 `target` 的节点,输出该节点在链表中的索引。此过程也属于线性查找。代码如下所示:
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="linked_list.py"
|
2023-10-06 14:10:18 +08:00
|
|
|
|
def find(head: ListNode, target: int) -> int:
|
|
|
|
|
"""在链表中查找值为 target 的首个节点"""
|
|
|
|
|
index = 0
|
|
|
|
|
while head:
|
|
|
|
|
if head.val == target:
|
|
|
|
|
return index
|
|
|
|
|
head = head.next
|
|
|
|
|
index += 1
|
|
|
|
|
return -1
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
=== "C++"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
```cpp title="linked_list.cpp"
|
2023-10-06 14:10:18 +08:00
|
|
|
|
/* 在链表中查找值为 target 的首个节点 */
|
|
|
|
|
int find(ListNode *head, int target) {
|
|
|
|
|
int index = 0;
|
|
|
|
|
while (head != nullptr) {
|
|
|
|
|
if (head->val == target)
|
|
|
|
|
return index;
|
|
|
|
|
head = head->next;
|
|
|
|
|
index++;
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
=== "Java"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
```java title="linked_list.java"
|
2023-10-06 14:10:18 +08:00
|
|
|
|
/* 在链表中查找值为 target 的首个节点 */
|
|
|
|
|
int find(ListNode head, int target) {
|
|
|
|
|
int index = 0;
|
|
|
|
|
while (head != null) {
|
|
|
|
|
if (head.val == target)
|
|
|
|
|
return index;
|
|
|
|
|
head = head.next;
|
|
|
|
|
index++;
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="linked_list.cs"
|
2023-10-06 14:10:18 +08:00
|
|
|
|
/* 在链表中查找值为 target 的首个节点 */
|
2023-11-27 02:32:06 +08:00
|
|
|
|
int Find(ListNode? head, int target) {
|
2023-10-06 14:10:18 +08:00
|
|
|
|
int index = 0;
|
|
|
|
|
while (head != null) {
|
|
|
|
|
if (head.val == target)
|
|
|
|
|
return index;
|
|
|
|
|
head = head.next;
|
|
|
|
|
index++;
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="linked_list.go"
|
2023-10-06 14:10:18 +08:00
|
|
|
|
/* 在链表中查找值为 target 的首个节点 */
|
|
|
|
|
func findNode(head *ListNode, target int) int {
|
|
|
|
|
index := 0
|
|
|
|
|
for head != nil {
|
|
|
|
|
if head.Val == target {
|
|
|
|
|
return index
|
|
|
|
|
}
|
|
|
|
|
head = head.Next
|
|
|
|
|
index++
|
|
|
|
|
}
|
|
|
|
|
return -1
|
|
|
|
|
}
|
2023-09-04 03:16:55 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="linked_list.swift"
|
2023-10-06 14:10:18 +08:00
|
|
|
|
/* 在链表中查找值为 target 的首个节点 */
|
|
|
|
|
func find(head: ListNode, target: Int) -> Int {
|
|
|
|
|
var head: ListNode? = head
|
|
|
|
|
var index = 0
|
|
|
|
|
while head != nil {
|
|
|
|
|
if head?.val == target {
|
|
|
|
|
return index
|
|
|
|
|
}
|
|
|
|
|
head = head?.next
|
|
|
|
|
index += 1
|
|
|
|
|
}
|
|
|
|
|
return -1
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
=== "JS"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
```javascript title="linked_list.js"
|
2023-10-06 14:10:18 +08:00
|
|
|
|
/* 在链表中查找值为 target 的首个节点 */
|
|
|
|
|
function find(head, target) {
|
|
|
|
|
let index = 0;
|
|
|
|
|
while (head !== null) {
|
|
|
|
|
if (head.val === target) {
|
|
|
|
|
return index;
|
|
|
|
|
}
|
|
|
|
|
head = head.next;
|
|
|
|
|
index += 1;
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2023-09-04 03:16:55 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "TS"
|
|
|
|
|
|
|
|
|
|
```typescript title="linked_list.ts"
|
2023-10-06 14:10:18 +08:00
|
|
|
|
/* 在链表中查找值为 target 的首个节点 */
|
|
|
|
|
function find(head: ListNode | null, target: number): number {
|
|
|
|
|
let index = 0;
|
|
|
|
|
while (head !== null) {
|
|
|
|
|
if (head.val === target) {
|
|
|
|
|
return index;
|
|
|
|
|
}
|
|
|
|
|
head = head.next;
|
|
|
|
|
index += 1;
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-06-02 02:38:24 +08:00
|
|
|
|
=== "Dart"
|
|
|
|
|
|
|
|
|
|
```dart title="linked_list.dart"
|
2023-10-06 14:10:18 +08:00
|
|
|
|
/* 在链表中查找值为 target 的首个节点 */
|
|
|
|
|
int find(ListNode? head, int target) {
|
|
|
|
|
int index = 0;
|
|
|
|
|
while (head != null) {
|
|
|
|
|
if (head.val == target) {
|
|
|
|
|
return index;
|
|
|
|
|
}
|
|
|
|
|
head = head.next;
|
|
|
|
|
index++;
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2023-06-02 02:38:24 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 10:57:40 +08:00
|
|
|
|
=== "Rust"
|
|
|
|
|
|
|
|
|
|
```rust title="linked_list.rs"
|
2023-10-06 14:10:18 +08:00
|
|
|
|
/* 在链表中查找值为 target 的首个节点 */
|
|
|
|
|
pub fn find<T: PartialEq>(head: Rc<RefCell<ListNode<T>>>, target: T, index: i32) -> i32 {
|
2024-03-18 03:11:07 +08:00
|
|
|
|
if head.borrow().val == target {
|
|
|
|
|
return index;
|
|
|
|
|
};
|
2023-10-06 14:10:18 +08:00
|
|
|
|
if let Some(node) = &head.borrow_mut().next {
|
|
|
|
|
return find(node.clone(), target, index + 1);
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2023-07-26 10:57:40 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="linked_list.c"
|
2023-10-06 14:10:18 +08:00
|
|
|
|
/* 在链表中查找值为 target 的首个节点 */
|
|
|
|
|
int find(ListNode *head, int target) {
|
|
|
|
|
int index = 0;
|
|
|
|
|
while (head) {
|
|
|
|
|
if (head->val == target)
|
|
|
|
|
return index;
|
|
|
|
|
head = head->next;
|
|
|
|
|
index++;
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2023-09-04 03:16:55 +08:00
|
|
|
|
```
|
|
|
|
|
|
2024-03-25 22:43:12 +08:00
|
|
|
|
=== "Kotlin"
|
|
|
|
|
|
|
|
|
|
```kotlin title="linked_list.kt"
|
|
|
|
|
/* 在链表中查找值为 target 的首个节点 */
|
|
|
|
|
fun find(head: ListNode?, target: Int): Int {
|
|
|
|
|
var index = 0
|
|
|
|
|
var h = head
|
|
|
|
|
while (h != null) {
|
|
|
|
|
if (h.value == target) return index
|
|
|
|
|
h = h.next
|
|
|
|
|
index++
|
|
|
|
|
}
|
|
|
|
|
return -1
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2024-03-31 03:53:04 +08:00
|
|
|
|
=== "Ruby"
|
|
|
|
|
|
|
|
|
|
```ruby title="linked_list.rb"
|
|
|
|
|
### 在链表中查找值为 target 的首个节点 ###
|
|
|
|
|
def find(head, target)
|
|
|
|
|
index = 0
|
|
|
|
|
while head
|
|
|
|
|
return index if head.val == target
|
|
|
|
|
head = head.next
|
|
|
|
|
index += 1
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-1
|
|
|
|
|
end
|
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="linked_list.zig"
|
2023-10-06 14:10:18 +08:00
|
|
|
|
// 在链表中查找值为 target 的首个节点
|
|
|
|
|
fn find(node: ?*inc.ListNode(i32), target: i32) i32 {
|
|
|
|
|
var head = node;
|
|
|
|
|
var index: i32 = 0;
|
|
|
|
|
while (head != null) {
|
|
|
|
|
if (head.?.val == target) return index;
|
|
|
|
|
head = head.?.next;
|
|
|
|
|
index += 1;
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2023-09-04 03:16:55 +08:00
|
|
|
|
```
|
|
|
|
|
|
2024-01-07 03:26:23 +08:00
|
|
|
|
??? pythontutor "可视化运行"
|
|
|
|
|
|
2024-01-09 16:00:24 +08:00
|
|
|
|
<div style="height: 549px; width: 100%;"><iframe class="pythontutor-iframe" src="https://pythontutor.com/iframe-embed.html#code=class%20ListNode%3A%0A%20%20%20%20%22%22%22%E9%93%BE%E8%A1%A8%E8%8A%82%E7%82%B9%E7%B1%BB%22%22%22%0A%20%20%20%20def%20__init__%28self,%20val%3A%20int%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%23%20%E8%8A%82%E7%82%B9%E5%80%BC%0A%20%20%20%20%20%20%20%20self.next%3A%20ListNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%90%8E%E7%BB%A7%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%0Adef%20find%28head%3A%20ListNode,%20target%3A%20int%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E5%9C%A8%E9%93%BE%E8%A1%A8%E4%B8%AD%E6%9F%A5%E6%89%BE%E5%80%BC%E4%B8%BA%20target%20%E7%9A%84%E9%A6%96%E4%B8%AA%E8%8A%82%E7%82%B9%22%22%22%0A%20%20%20%20index%20%3D%200%0A%20%20%20%20while%20head%3A%0A%20%20%20%20%20%20%20%20if%20head.val%20%3D%3D%20target%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20index%0A%20%20%20%20%20%20%20%20head%20%3D%20head.next%0A%20%20%20%20%20%20%20%20index%20%2B%3D%201%0A%20%20%20%20return%20-1%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E9%93%BE%E8%A1%A8%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%90%84%E4%B8%AA%E8%8A%82%E7%82%B9%0A%20%20%20%20n0%20%3D%20ListNode%281%29%0A%20%20%20%20n1%20%3D%20ListNode%283%29%0A%20%20%20%20n2%20%3D%20ListNode%282%29%0A%20%20%20%20n3%20%3D%20ListNode%285%29%0A%20%20%20%20n4%20%3D%20ListNode%284%29%0A%20%20%20%20%23%20%E6%9E%84%E5%BB%BA%E8%8A%82%E7%82%B9%E4%B9%8B%E9%97%B4%E7%9A%84%E5%BC%95%E7%94%A8%0A%20%20%20%20n0.next%20%3D%20n1%0A%20%20%20%20n1.next%20%3D%20n2%0A%20%20%20%20n2.next%20%3D%20n3%0A%20%20%20%20n3.next%20%3D%20n4%0A%0A%20%20%20%20%23%20%E6%9F%A5%E6%89%BE%E8%8A%82%E7%82%B9%0A%20%20%20%20index%20%3D%20find%28n0,%202%29%0A%20%20%20%20print%28%22%E9%93%BE%E8%A1%A8%E4%B8%AD%E5%80%BC%E4%B8%BA%202%20%E7%9A%84%E8%8A%82%E7%82%B9%E7%9A%84%E7%B4%A2%E5%BC%95%20%3D%20%7B%7D%22.format%28index%29%29&codeDivHeight=472&codeDivWidth=350&cumulative=false&curInstr=34&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false"> </iframe></div>
|
|
|
|
|
<div style="margin-top: 5px;"><a href="https://pythontutor.com/iframe-embed.html#code=class%20ListNode%3A%0A%20%20%20%20%22%22%22%E9%93%BE%E8%A1%A8%E8%8A%82%E7%82%B9%E7%B1%BB%22%22%22%0A%20%20%20%20def%20__init__%28self,%20val%3A%20int%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%23%20%E8%8A%82%E7%82%B9%E5%80%BC%0A%20%20%20%20%20%20%20%20self.next%3A%20ListNode%20%7C%20None%20%3D%20None%20%20%23%20%E5%90%8E%E7%BB%A7%E8%8A%82%E7%82%B9%E5%BC%95%E7%94%A8%0A%0Adef%20find%28head%3A%20ListNode,%20target%3A%20int%29%20-%3E%20int%3A%0A%20%20%20%20%22%22%22%E5%9C%A8%E9%93%BE%E8%A1%A8%E4%B8%AD%E6%9F%A5%E6%89%BE%E5%80%BC%E4%B8%BA%20target%20%E7%9A%84%E9%A6%96%E4%B8%AA%E8%8A%82%E7%82%B9%22%22%22%0A%20%20%20%20index%20%3D%200%0A%20%20%20%20while%20head%3A%0A%20%20%20%20%20%20%20%20if%20head.val%20%3D%3D%20target%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20index%0A%20%20%20%20%20%20%20%20head%20%3D%20head.next%0A%20%20%20%20%20%20%20%20index%20%2B%3D%201%0A%20%20%20%20return%20-1%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E9%93%BE%E8%A1%A8%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%90%84%E4%B8%AA%E8%8A%82%E7%82%B9%0A%20%20%20%20n0%20%3D%20ListNode%281%29%0A%20%20%20%20n1%20%3D%20ListNode%283%29%0A%20%20%20%20n2%20%3D%20ListNode%282%29%0A%20%20%20%20n3%20%3D%20ListNode%285%29%0A%20%20%20%20n4%20%3D%20ListNode%284%29%0A%20%20%20%20%23%20%E6%9E%84%E5%BB%BA%E8%8A%82%E7%82%B9%E4%B9%8B%E9%97%B4%E7%9A%84%E5%BC%95%E7%94%A8%0A%20%20%20%20n0.next%20%3D%20n1%0A%20%20%20%20n1.next%20%3D%20n2%0A%20%20%20%20n2.next%20%3D%20n3%0A%20%20%20%20n3.next%20%3D%20n4%0A%0A%20%20%20%20%23%20%E6%9F%A5%E6%89%BE%E8%8A%82%E7%82%B9%0A%20%20%20%20index%20%3D%20find%28n0,%202%29%0A%20%20%20%20print%28%22%E9%93%BE%E8%A1%A8%E4%B8%AD%E5%80%BC%E4%B8%BA%202%20%E7%9A%84%E8%8A%82%E7%82%B9%E7%9A%84%E7%B4%A2%E5%BC%95%20%3D%20%7B%7D%22.format%28index%29%29&codeDivHeight=800&codeDivWidth=600&cumulative=false&curInstr=34&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false" target="_blank" rel="noopener noreferrer">全屏观看 ></a></div>
|
2024-01-07 03:26:23 +08:00
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
## 4.2.2 数组 vs. 链表
|
2023-08-17 05:12:05 +08:00
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
表 4-1 总结了数组和链表的各项特点并对比了操作效率。由于它们采用两种相反的存储策略,因此各种性质和操作效率也呈现对立的特点。
|
2023-08-17 05:12:05 +08:00
|
|
|
|
|
2023-08-22 13:50:12 +08:00
|
|
|
|
<p align="center"> 表 4-1 数组与链表的效率对比 </p>
|
2023-08-19 19:21:18 +08:00
|
|
|
|
|
2023-08-17 05:12:05 +08:00
|
|
|
|
<div class="center-table" markdown>
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-11-26 02:06:39 +08:00
|
|
|
|
| | 数组 | 链表 |
|
|
|
|
|
| -------- | ------------------------------ | -------------- |
|
|
|
|
|
| 存储方式 | 连续内存空间 | 分散内存空间 |
|
|
|
|
|
| 容量扩展 | 长度不可变 | 可灵活扩展 |
|
|
|
|
|
| 内存效率 | 元素占用内存少、但可能浪费空间 | 元素占用内存多 |
|
|
|
|
|
| 访问元素 | $O(1)$ | $O(n)$ |
|
|
|
|
|
| 添加元素 | $O(n)$ | $O(1)$ |
|
|
|
|
|
| 删除元素 | $O(n)$ | $O(1)$ |
|
2023-08-17 05:12:05 +08:00
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
2023-08-19 22:07:27 +08:00
|
|
|
|
## 4.2.3 常见链表类型
|
2023-08-17 05:12:05 +08:00
|
|
|
|
|
2023-08-22 13:50:12 +08:00
|
|
|
|
如图 4-8 所示,常见的链表类型包括三种。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-12-28 17:18:37 +08:00
|
|
|
|
- **单向链表**:即前面介绍的普通链表。单向链表的节点包含值和指向下一节点的引用两项数据。我们将首个节点称为头节点,将最后一个节点称为尾节点,尾节点指向空 `None` 。
|
2023-12-02 06:24:05 +08:00
|
|
|
|
- **环形链表**:如果我们令单向链表的尾节点指向头节点(首尾相接),则得到一个环形链表。在环形链表中,任意节点都可以视作头节点。
|
2023-08-21 19:32:37 +08:00
|
|
|
|
- **双向链表**:与单向链表相比,双向链表记录了两个方向的引用。双向链表的节点定义同时包含指向后继节点(下一个节点)和前驱节点(上一个节点)的引用(指针)。相较于单向链表,双向链表更具灵活性,可以朝两个方向遍历链表,但相应地也需要占用更多的内存空间。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
=== "Python"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
```python title=""
|
|
|
|
|
class ListNode:
|
|
|
|
|
"""双向链表节点类"""
|
|
|
|
|
def __init__(self, val: int):
|
2023-09-21 20:44:38 +08:00
|
|
|
|
self.val: int = val # 节点值
|
|
|
|
|
self.next: ListNode | None = None # 指向后继节点的引用
|
|
|
|
|
self.prev: ListNode | None = None # 指向前驱节点的引用
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title=""
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 双向链表节点结构体 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
struct ListNode {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
int val; // 节点值
|
2023-08-17 05:12:05 +08:00
|
|
|
|
ListNode *next; // 指向后继节点的指针
|
|
|
|
|
ListNode *prev; // 指向前驱节点的指针
|
2023-02-08 15:20:18 +08:00
|
|
|
|
ListNode(int x) : val(x), next(nullptr), prev(nullptr) {} // 构造函数
|
|
|
|
|
};
|
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
=== "Java"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
```java title=""
|
|
|
|
|
/* 双向链表节点类 */
|
|
|
|
|
class ListNode {
|
|
|
|
|
int val; // 节点值
|
|
|
|
|
ListNode next; // 指向后继节点的引用
|
|
|
|
|
ListNode prev; // 指向前驱节点的引用
|
|
|
|
|
ListNode(int x) { val = x; } // 构造函数
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title=""
|
|
|
|
|
/* 双向链表节点类 */
|
2023-11-27 02:32:06 +08:00
|
|
|
|
class ListNode(int x) { // 构造函数
|
|
|
|
|
int val = x; // 节点值
|
2023-09-04 03:16:55 +08:00
|
|
|
|
ListNode next; // 指向后继节点的引用
|
|
|
|
|
ListNode prev; // 指向前驱节点的引用
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title=""
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 双向链表节点结构体 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
type DoublyListNode struct {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
Val int // 节点值
|
2023-08-17 05:12:05 +08:00
|
|
|
|
Next *DoublyListNode // 指向后继节点的指针
|
|
|
|
|
Prev *DoublyListNode // 指向前驱节点的指针
|
2023-02-08 15:20:18 +08:00
|
|
|
|
}
|
2023-08-17 05:12:05 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
// NewDoublyListNode 初始化
|
|
|
|
|
func NewDoublyListNode(val int) *DoublyListNode {
|
|
|
|
|
return &DoublyListNode{
|
|
|
|
|
Val: val,
|
|
|
|
|
Next: nil,
|
|
|
|
|
Prev: nil,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title=""
|
|
|
|
|
/* 双向链表节点类 */
|
|
|
|
|
class ListNode {
|
|
|
|
|
var val: Int // 节点值
|
|
|
|
|
var next: ListNode? // 指向后继节点的引用
|
|
|
|
|
var prev: ListNode? // 指向前驱节点的引用
|
|
|
|
|
|
|
|
|
|
init(x: Int) { // 构造函数
|
|
|
|
|
val = x
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:34:46 +08:00
|
|
|
|
=== "JS"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
```javascript title=""
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 双向链表节点类 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
class ListNode {
|
2023-05-27 17:14:08 +08:00
|
|
|
|
constructor(val, next, prev) {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
this.val = val === undefined ? 0 : val; // 节点值
|
2023-08-17 05:12:05 +08:00
|
|
|
|
this.next = next === undefined ? null : next; // 指向后继节点的引用
|
|
|
|
|
this.prev = prev === undefined ? null : prev; // 指向前驱节点的引用
|
2023-02-08 15:20:18 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:34:46 +08:00
|
|
|
|
=== "TS"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
```typescript title=""
|
2023-04-09 04:34:58 +08:00
|
|
|
|
/* 双向链表节点类 */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
class ListNode {
|
|
|
|
|
val: number;
|
|
|
|
|
next: ListNode | null;
|
|
|
|
|
prev: ListNode | null;
|
|
|
|
|
constructor(val?: number, next?: ListNode | null, prev?: ListNode | null) {
|
2023-04-09 04:34:58 +08:00
|
|
|
|
this.val = val === undefined ? 0 : val; // 节点值
|
2023-08-17 05:12:05 +08:00
|
|
|
|
this.next = next === undefined ? null : next; // 指向后继节点的引用
|
|
|
|
|
this.prev = prev === undefined ? null : prev; // 指向前驱节点的引用
|
2023-02-08 15:20:18 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-09-04 03:16:55 +08:00
|
|
|
|
=== "Dart"
|
|
|
|
|
|
|
|
|
|
```dart title=""
|
|
|
|
|
/* 双向链表节点类 */
|
|
|
|
|
class ListNode {
|
|
|
|
|
int val; // 节点值
|
|
|
|
|
ListNode next; // 指向后继节点的引用
|
|
|
|
|
ListNode prev; // 指向前驱节点的引用
|
|
|
|
|
ListNode(this.val, [this.next, this.prev]); // 构造函数
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Rust"
|
|
|
|
|
|
|
|
|
|
```rust title=""
|
|
|
|
|
use std::rc::Rc;
|
|
|
|
|
use std::cell::RefCell;
|
|
|
|
|
|
|
|
|
|
/* 双向链表节点类型 */
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
struct ListNode {
|
|
|
|
|
val: i32, // 节点值
|
|
|
|
|
next: Option<Rc<RefCell<ListNode>>>, // 指向后继节点的指针
|
|
|
|
|
prev: Option<Rc<RefCell<ListNode>>>, // 指向前驱节点的指针
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 构造函数 */
|
|
|
|
|
impl ListNode {
|
|
|
|
|
fn new(val: i32) -> Self {
|
|
|
|
|
ListNode {
|
|
|
|
|
val,
|
|
|
|
|
next: None,
|
|
|
|
|
prev: None,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title=""
|
2023-04-18 20:19:07 +08:00
|
|
|
|
/* 双向链表节点结构体 */
|
2023-10-18 02:16:55 +08:00
|
|
|
|
typedef struct ListNode {
|
2023-04-18 20:19:07 +08:00
|
|
|
|
int val; // 节点值
|
2023-08-17 05:12:05 +08:00
|
|
|
|
struct ListNode *next; // 指向后继节点的指针
|
|
|
|
|
struct ListNode *prev; // 指向前驱节点的指针
|
2023-10-18 02:16:55 +08:00
|
|
|
|
} ListNode;
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-18 20:19:07 +08:00
|
|
|
|
/* 构造函数 */
|
|
|
|
|
ListNode *newListNode(int val) {
|
2023-11-14 21:31:21 +08:00
|
|
|
|
ListNode *node;
|
2023-04-18 20:19:07 +08:00
|
|
|
|
node = (ListNode *) malloc(sizeof(ListNode));
|
|
|
|
|
node->val = val;
|
|
|
|
|
node->next = NULL;
|
|
|
|
|
node->prev = NULL;
|
|
|
|
|
return node;
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
2024-03-25 22:43:12 +08:00
|
|
|
|
=== "Kotlin"
|
|
|
|
|
|
|
|
|
|
```kotlin title=""
|
2024-03-31 03:53:04 +08:00
|
|
|
|
/* 双向链表节点类 */
|
|
|
|
|
// 构造方法
|
|
|
|
|
class ListNode(x: Int) {
|
|
|
|
|
val _val: Int = x // 节点值
|
|
|
|
|
val next: ListNode? = null // 指向后继节点的引用
|
|
|
|
|
val prev: ListNode? = null // 指向前驱节点的引用
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Ruby"
|
|
|
|
|
|
|
|
|
|
```ruby title=""
|
|
|
|
|
# 双向链表节点类
|
|
|
|
|
class ListNode
|
|
|
|
|
attr_accessor :val # 节点值
|
|
|
|
|
attr_accessor :next # 指向后继节点的引用
|
|
|
|
|
attr_accessor :prev # 指向前驱节点的引用
|
2024-03-25 22:43:12 +08:00
|
|
|
|
|
2024-04-01 05:25:50 +08:00
|
|
|
|
def initialize(val=0, next_node=nil, prev_node=nil)
|
|
|
|
|
@val = val
|
2024-03-31 03:53:04 +08:00
|
|
|
|
@next = next_node
|
|
|
|
|
@prev = prev_node
|
|
|
|
|
end
|
|
|
|
|
end
|
2024-03-25 22:43:12 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title=""
|
2023-04-09 04:34:58 +08:00
|
|
|
|
// 双向链表节点类
|
2023-02-08 15:20:18 +08:00
|
|
|
|
pub fn ListNode(comptime T: type) type {
|
|
|
|
|
return struct {
|
|
|
|
|
const Self = @This();
|
2023-08-17 05:12:05 +08:00
|
|
|
|
|
2023-04-09 04:34:58 +08:00
|
|
|
|
val: T = 0, // 节点值
|
2023-08-17 05:12:05 +08:00
|
|
|
|
next: ?*Self = null, // 指向后继节点的指针
|
|
|
|
|
prev: ?*Self = null, // 指向前驱节点的指针
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
// 构造函数
|
|
|
|
|
pub fn init(self: *Self, x: i32) void {
|
|
|
|
|
self.val = x;
|
|
|
|
|
self.next = null;
|
|
|
|
|
self.prev = null;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-11-09 05:13:48 +08:00
|
|
|
|
![常见链表种类](linked_list.assets/linkedlist_common_types.png){ class="animation-figure" }
|
2023-02-26 19:53:26 +08:00
|
|
|
|
|
2023-08-22 13:50:12 +08:00
|
|
|
|
<p align="center"> 图 4-8 常见链表种类 </p>
|
2023-07-19 16:10:06 +08:00
|
|
|
|
|
2023-08-19 22:07:27 +08:00
|
|
|
|
## 4.2.4 链表典型应用
|
2023-07-19 16:10:06 +08:00
|
|
|
|
|
2023-08-21 03:56:41 +08:00
|
|
|
|
单向链表通常用于实现栈、队列、哈希表和图等数据结构。
|
2023-07-19 16:10:06 +08:00
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
- **栈与队列**:当插入和删除操作都在链表的一端进行时,它表现出先进后出的特性,对应栈;当插入操作在链表的一端进行,删除操作在链表的另一端进行,它表现出先进先出的特性,对应队列。
|
|
|
|
|
- **哈希表**:链式地址是解决哈希冲突的主流方案之一,在该方案中,所有冲突的元素都会被放到一个链表中。
|
|
|
|
|
- **图**:邻接表是表示图的一种常用方式,其中图的每个顶点都与一个链表相关联,链表中的每个元素都代表与该顶点相连的其他顶点。
|
2023-07-19 16:10:06 +08:00
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
双向链表常用于需要快速查找前一个和后一个元素的场景。
|
2023-07-19 16:10:06 +08:00
|
|
|
|
|
2023-08-17 05:12:05 +08:00
|
|
|
|
- **高级数据结构**:比如在红黑树、B 树中,我们需要访问节点的父节点,这可以通过在节点中保存一个指向父节点的引用来实现,类似于双向链表。
|
2023-07-19 16:10:06 +08:00
|
|
|
|
- **浏览器历史**:在网页浏览器中,当用户点击前进或后退按钮时,浏览器需要知道用户访问过的前一个和后一个网页。双向链表的特性使得这种操作变得简单。
|
2023-12-02 06:24:05 +08:00
|
|
|
|
- **LRU 算法**:在缓存淘汰(LRU)算法中,我们需要快速找到最近最少使用的数据,以及支持快速添加和删除节点。这时候使用双向链表就非常合适。
|
2023-07-19 16:10:06 +08:00
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
环形链表常用于需要周期性操作的场景,比如操作系统的资源调度。
|
2023-07-19 16:10:06 +08:00
|
|
|
|
|
2023-12-02 06:24:05 +08:00
|
|
|
|
- **时间片轮转调度算法**:在操作系统中,时间片轮转调度算法是一种常见的 CPU 调度算法,它需要对一组进程进行循环。每个进程被赋予一个时间片,当时间片用完时,CPU 将切换到下一个进程。这种循环操作可以通过环形链表来实现。
|
|
|
|
|
- **数据缓冲区**:在某些数据缓冲区的实现中,也可能会使用环形链表。比如在音频、视频播放器中,数据流可能会被分成多个缓冲块并放入一个环形链表,以便实现无缝播放。
|