mirror of
https://github.com/krahets/hello-algo.git
synced 2025-07-25 11:13:38 +08:00
feat(linked_list): add the Go code to linked_list docs (Chapter of Array and LinkedList)
This commit is contained in:
@ -51,7 +51,19 @@ comments: true
|
||||
=== "Go"
|
||||
|
||||
```go title=""
|
||||
|
||||
/* 链表结点结构体 */
|
||||
type ListNode struct {
|
||||
Val int // 结点值
|
||||
Next *ListNode // 指向下一结点的指针(引用)
|
||||
}
|
||||
|
||||
// NewListNode 构造函数,创建一个新的链表
|
||||
func NewListNode(val int) *ListNode {
|
||||
return &ListNode{
|
||||
Val: val,
|
||||
Next: nil,
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
=== "JavaScript"
|
||||
@ -162,7 +174,19 @@ comments: true
|
||||
=== "Go"
|
||||
|
||||
```go title=""
|
||||
/* 初始化链表 1 -> 3 -> 2 -> 5 -> 4 */
|
||||
// 初始化各个结点
|
||||
n0 := NewListNode(1)
|
||||
n1 := NewListNode(3)
|
||||
n2 := NewListNode(2)
|
||||
n3 := NewListNode(5)
|
||||
n4 := NewListNode(4)
|
||||
|
||||
// 构建引用指向
|
||||
n0.Next = n1
|
||||
n1.Next = n2
|
||||
n2.Next = n3
|
||||
n3.Next = n4
|
||||
```
|
||||
|
||||
=== "JavaScript"
|
||||
@ -294,7 +318,23 @@ comments: true
|
||||
=== "Go"
|
||||
|
||||
```go title=""
|
||||
/* 在链表的结点 n0 之后插入结点 P */
|
||||
func insert(n0 *ListNode, P *ListNode) {
|
||||
n1 := n0.Next
|
||||
n0.Next = P
|
||||
P.Next = n1
|
||||
}
|
||||
|
||||
/* 删除链表的结点 n0 之后的首个结点 */
|
||||
func removeNode(n0 *ListNode) {
|
||||
if n0.Next == nil {
|
||||
return
|
||||
}
|
||||
// n0 -> P -> n1
|
||||
P := n0.Next
|
||||
n1 := P.Next
|
||||
n0.Next = n1
|
||||
}
|
||||
```
|
||||
|
||||
=== "JavaScript"
|
||||
@ -415,7 +455,16 @@ comments: true
|
||||
=== "Go"
|
||||
|
||||
```go title=""
|
||||
|
||||
/* 访问链表中索引为 index 的结点 */
|
||||
func access(head *ListNode, index int) *ListNode {
|
||||
for i := 0; i < index; i++ {
|
||||
head = head.Next
|
||||
if head == nil {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return head
|
||||
}
|
||||
```
|
||||
|
||||
=== "JavaScript"
|
||||
@ -524,7 +573,18 @@ comments: true
|
||||
=== "Go"
|
||||
|
||||
```go title=""
|
||||
|
||||
/* 在链表中查找值为 target 的首个结点 */
|
||||
func find(head *ListNode, target int) int {
|
||||
index := 0
|
||||
for head != nil {
|
||||
if head.Val == target {
|
||||
return index
|
||||
}
|
||||
head = head.Next
|
||||
index++
|
||||
}
|
||||
return -1
|
||||
}
|
||||
```
|
||||
|
||||
=== "JavaScript"
|
||||
@ -631,7 +691,21 @@ comments: true
|
||||
=== "Go"
|
||||
|
||||
```go title=""
|
||||
|
||||
/* 双向链表结点结构体 */
|
||||
type DoublyListNode struct {
|
||||
Val int // 结点值
|
||||
Next *DoublyListNode // 指向后继结点的指针(引用)
|
||||
Prev *DoublyListNode // 指向前驱结点的指针(引用)
|
||||
}
|
||||
|
||||
// NewDoublyListNode 初始化
|
||||
func NewDoublyListNode(val int) *DoublyListNode {
|
||||
return &DoublyListNode{
|
||||
Val: val,
|
||||
Next: nil,
|
||||
Prev: nil,
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
=== "JavaScript"
|
||||
|
Reference in New Issue
Block a user