对0707设计链表,go版本单链表做了修改,原来版本没有删除链表指定index下标的方法,并且已有的在leetcode执行有误,做了修改调整,基于原来的代码

This commit is contained in:
Feegg
2023-06-11 02:13:01 +08:00
parent 6e5bffbc5b
commit a32bbeb71a

View File

@ -663,100 +663,128 @@ Go
//单链表实现 //单链表实现
package main package main
import "fmt" import (
"fmt"
func main() { )
var list = new(SingleLinkedList)
list.Init()
list.addAtHead(100)
list.addAtTail(242)
list.addAtTail(777)
list.addAtIndex(1, 99999)
list.printLinkedList()
}
// 单链表写法 //
type SingleNode struct { type SingleNode struct {
Val int Val int // 节点的值
Next *SingleNode Next *SingleNode // 下一个节点的指针
} }
type SingleLinkedList struct { type MyLinkedList struct {
dummyHead *SingleNode dummyHead *SingleNode // 虚拟头节点
Size int Size int // 链表大小
} }
// 初始化链表 func main() {
func (list *SingleLinkedList) Init() *SingleLinkedList { list := Constructor() // 初始化链表
list.Size = 0 list.AddAtHead(100) // 在头部添加元素
list.dummyHead = new(SingleNode) list.AddAtTail(242) // 在尾部添加元素
return list list.AddAtTail(777) // 在尾部添加元素
list.AddAtIndex(1, 99999) // 在指定位置添加元素
list.printLinkedList() // 打印链表
} }
// 获取第index个节点数值 /** Initialize your data structure here. */
func (list *SingleLinkedList) get(index int) int { func Constructor() MyLinkedList {
if list != nil || index < 0 || index > list.Size { newNode := &SingleNode{ // 创建新节点
-999,
nil,
}
return MyLinkedList{ // 返回链表
dummyHead: newNode,
Size: 0,
}
}
/** Get the value of the index-th node in the linked list. If the index is
invalid, return -1. */
func (this *MyLinkedList) Get(index int) int {
/*if this != nil || index < 0 || index > this.Size {
return -1
}*/
if this == nil || index < 0 || index >= this.Size { // 如果索引无效则返回-1
return -1 return -1
} }
// 让cur等于真正头节点 // 让cur等于真正头节点
cur := list.dummyHead.Next cur := this.dummyHead.Next // 设置当前节点为真实头节点
for i := 0; i < index; i++ { for i := 0; i < index; i++ { // 遍历到索引所在的节点
cur = cur.Next cur = cur.Next
} }
return cur.Val return cur.Val // 返回节点值
} }
// 在链表最前面插入一个节点 /** Add a node of value val before the first element of the linked list. After
func (list *SingleLinkedList) addAtHead(val int) { the insertion, the new node will be the first node of the linked list. */
func (this *MyLinkedList) AddAtHead(val int) {
// 以下两行代码可用一行代替 // 以下两行代码可用一行代替
// newNode := new(SingleNode) // newNode := new(SingleNode)
// newNode.Val = val // newNode.Val = val
newNode := &SingleNode{Val: val} newNode := &SingleNode{Val: val} // 创建新节点
newNode.Next = this.dummyHead.Next // 新节点指向当前头节点
newNode.Next = list.dummyHead.Next this.dummyHead.Next = newNode // 新节点变为头节点
list.dummyHead.Next = newNode this.Size++ // 链表大小增加1
list.Size++
} }
// 在链表最后面插入一个节点 /** Append a node of value val to the last element of the linked list. */
func (list *SingleLinkedList) addAtTail(val int) { func (this *MyLinkedList) AddAtTail(val int) {
newNode := &SingleNode{Val: val} newNode := &SingleNode{Val: val} // 创建新节点
cur := list.dummyHead cur := this.dummyHead // 设置当前节点为虚拟头节点
for cur.Next != nil { for cur.Next != nil { // 遍历到最后一个节点
cur = cur.Next cur = cur.Next
} }
cur.Next = newNode cur.Next = newNode // 在尾部添加新节点
list.Size++ this.Size++ // 链表大小增加1
} }
// 打印链表 /** Add a node of value val before the index-th node in the linked list. If
func (list *SingleLinkedList) printLinkedList() { index equals to the length of linked list, the node will be appended to the
cur := list.dummyHead end of linked list. If index is greater than the length, the node will not be
for cur.Next != nil { inserted. */
fmt.Println(cur.Next.Val) func (this *MyLinkedList) AddAtIndex(index int, val int) {
cur = cur.Next if index < 0 { // 如果索引小于0设置为0
}
}
// 在第index个节点之前插入新节点
func (list *SingleLinkedList) addAtIndex(index int, val int) {
if index < 0 {
index = 0 index = 0
} else if index > list.Size { } else if index > this.Size { // 如果索引大于链表长度,直接返回
return return
} }
newNode := &SingleNode{Val: val} newNode := &SingleNode{Val: val} // 创建新节点
cur := list.dummyHead //用虚拟头节点不用考虑在头部插入的情况 cur := this.dummyHead // 设置当前节点为虚拟头节点
for i := 0; i < index; i++ { for i := 0; i < index; i++ { // 遍历到指定索引的前一个节点
cur = cur.Next cur = cur.Next
} }
newNode.Next = cur.Next newNode.Next = cur.Next // 新节点指向原索引节点
cur.Next = newNode cur.Next = newNode // 原索引的前一个节点指向新节点
list.Size++ this.Size++ // 链表大小增加1
} }
/** Delete the index-th node in the linked list, if the index is valid. */
func (this *MyLinkedList) DeleteAtIndex(index int) {
if index < 0 || index >= this.Size { // 如果索引无效则直接返回
return
}
cur := this.dummyHead // 设置当前节点为虚拟头节点
for i := 0; i < index; i++ { // 遍历到要删除节点的前一个节点
cur = cur.Next
}
if cur.Next != nil {
cur.Next = cur.Next.Next // 当前节点直接指向下下个节点,即删除了下一个节点
}
this.Size-- // 注意删除节点后应将链表大小减一
}
// 打印链表
func (list *MyLinkedList) printLinkedList() {
cur := list.dummyHead // 设置当前节点为虚拟头节点
for cur.Next != nil { // 遍历链表
fmt.Println(cur.Next.Val) // 打印节点值
cur = cur.Next // 切换到下一个节点
}
}
``` ```
```go ```go