Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4aab5bf

Browse files
Merge pull request youngyangyang04#2132 from RS-maker975/master
对0707设计链表,go版本单链表做了修改,原来版本没有删除链表指定index下标的方法,并且已有的在leetcode执行有误,做了修改调...
2 parents 496f80d + a32bbeb commit 4aab5bf

File tree

1 file changed

+87
-59
lines changed

1 file changed

+87
-59
lines changed

‎problems/0707.设计链表.md‎

Lines changed: 87 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -663,100 +663,128 @@ Go:
663663
//单链表实现
664664
package main
665665

666-
import "fmt"
666+
import (
667+
"fmt"
668+
)
667669

668-
func main() {
669-
var list = new(SingleLinkedList)
670-
list.Init()
671-
list.addAtHead(100)
672-
list.addAtTail(242)
673-
list.addAtTail(777)
674-
list.addAtIndex(1, 99999)
675-
list.printLinkedList()
670+
type SingleNode struct {
671+
Val int // 节点的值
672+
Next *SingleNode // 下一个节点的指针
676673
}
677674

678-
// 单链表写法 //
679-
680-
type SingleNode struct {
681-
Val int
682-
Next *SingleNode
675+
type MyLinkedList struct {
676+
dummyHead *SingleNode // 虚拟头节点
677+
Size int // 链表大小
683678
}
684679

685-
type SingleLinkedList struct {
686-
dummyHead *SingleNode
687-
Size int
680+
func main() {
681+
list := Constructor() // 初始化链表
682+
list.AddAtHead(100) // 在头部添加元素
683+
list.AddAtTail(242) // 在尾部添加元素
684+
list.AddAtTail(777) // 在尾部添加元素
685+
list.AddAtIndex(1, 99999) // 在指定位置添加元素
686+
list.printLinkedList() // 打印链表
688687
}
689688

690-
// 初始化链表
691-
func (list *SingleLinkedList) Init() *SingleLinkedList {
692-
list.Size = 0
693-
list.dummyHead = new(SingleNode)
694-
return list
689+
/** Initialize your data structure here. */
690+
func Constructor() MyLinkedList {
691+
newNode := &SingleNode{ // 创建新节点
692+
-999,
693+
nil,
694+
}
695+
return MyLinkedList{ // 返回链表
696+
dummyHead: newNode,
697+
Size: 0,
698+
}
699+
695700
}
696701

697-
// 获取第index个节点数值
698-
func (list *SingleLinkedList) get(index int) int {
699-
if list != nil || index < 0 || index > list.Size {
702+
/** Get the value of the index-th node in the linked list. If the index is
703+
invalid, return -1. */
704+
func (this *MyLinkedList) Get(index int) int {
705+
/*if this != nil || index < 0 || index > this.Size {
706+
return -1
707+
}*/
708+
if this == nil || index < 0 || index >= this.Size { // 如果索引无效则返回-1
700709
return -1
701710
}
702711
// 让cur等于真正头节点
703-
cur := list.dummyHead.Next
704-
for i := 0; i < index; i++ {
712+
cur := this.dummyHead.Next// 设置当前节点为真实头节点
713+
for i := 0; i < index; i++ {// 遍历到索引所在的节点
705714
cur = cur.Next
706715
}
707-
return cur.Val
716+
return cur.Val// 返回节点值
708717
}
709718

710-
// 在链表最前面插入一个节点
711-
func (list *SingleLinkedList) addAtHead(val int) {
719+
/** Add a node of value val before the first element of the linked list. After
720+
the insertion, the new node will be the first node of the linked list. */
721+
func (this *MyLinkedList) AddAtHead(val int) {
712722
// 以下两行代码可用一行代替
713723
// newNode := new(SingleNode)
714724
// newNode.Val = val
715-
newNode := &SingleNode{Val: val}
716-
717-
newNode.Next = list.dummyHead.Next
718-
list.dummyHead.Next = newNode
719-
list.Size++
725+
newNode := &SingleNode{Val: val} // 创建新节点
726+
newNode.Next = this.dummyHead.Next // 新节点指向当前头节点
727+
this.dummyHead.Next = newNode // 新节点变为头节点
728+
this.Size++ // 链表大小增加1
720729
}
721730

722-
// 在链表最后面插入一个节点
723-
func (list *SingleLinkedList) addAtTail(val int) {
724-
newNode := &SingleNode{Val: val}
725-
cur := list.dummyHead
726-
for cur.Next != nil {
731+
/** Append a node of value val to the last element of the linked list. */
732+
func (this *MyLinkedList) AddAtTail(val int) {
733+
newNode := &SingleNode{Val: val}// 创建新节点
734+
cur := this.dummyHead// 设置当前节点为虚拟头节点
735+
for cur.Next != nil {// 遍历到最后一个节点
727736
cur = cur.Next
728737
}
729-
cur.Next = newNode
730-
list.Size++
738+
cur.Next = newNode// 在尾部添加新节点
739+
this.Size++// 链表大小增加1
731740
}
732741

733-
// 打印链表
734-
func (list *SingleLinkedList) printLinkedList() {
735-
cur := list.dummyHead
736-
for cur.Next != nil {
737-
fmt.Println(cur.Next.Val)
742+
/** Add a node of value val before the index-th node in the linked list. If
743+
index equals to the length of linked list, the node will be appended to the
744+
end of linked list. If index is greater than the length, the node will not be
745+
inserted. */
746+
func (this *MyLinkedList) AddAtIndex(index int, val int) {
747+
if index < 0 { // 如果索引小于0,设置为0
748+
index = 0
749+
} else if index > this.Size { // 如果索引大于链表长度,直接返回
750+
return
751+
}
752+
753+
newNode := &SingleNode{Val: val} // 创建新节点
754+
cur := this.dummyHead // 设置当前节点为虚拟头节点
755+
for i := 0; i < index; i++ { // 遍历到指定索引的前一个节点
738756
cur = cur.Next
739757
}
758+
newNode.Next = cur.Next // 新节点指向原索引节点
759+
cur.Next = newNode // 原索引的前一个节点指向新节点
760+
this.Size++ // 链表大小增加1
740761
}
741762

742-
// 在第index个节点之前插入新节点
743-
func (list *SingleLinkedList) addAtIndex(index int, val int) {
744-
if index < 0 {
745-
index = 0
746-
} else if index > list.Size {
763+
/** Delete the index-th node in the linked list, if the index is valid. */
764+
func (this *MyLinkedList) DeleteAtIndex(index int) {
765+
if index < 0 || index >= this.Size { // 如果索引无效则直接返回
747766
return
748767
}
749-
750-
newNode := &SingleNode{Val: val}
751-
cur := list.dummyHead //用虚拟头节点不用考虑在头部插入的情况
752-
for i := 0; i < index; i++ {
768+
cur := this.dummyHead // 设置当前节点为虚拟头节点
769+
for i := 0; i < index; i++ { // 遍历到要删除节点的前一个节点
753770
cur = cur.Next
754771
}
755-
newNode.Next = cur.Next
756-
cur.Next = newNode
757-
list.Size++
772+
if cur.Next != nil {
773+
cur.Next = cur.Next.Next // 当前节点直接指向下下个节点,即删除了下一个节点
774+
}
775+
this.Size-- // 注意删除节点后应将链表大小减一
776+
}
777+
778+
// 打印链表
779+
func (list *MyLinkedList) printLinkedList() {
780+
cur := list.dummyHead // 设置当前节点为虚拟头节点
781+
for cur.Next != nil { // 遍历链表
782+
fmt.Println(cur.Next.Val) // 打印节点值
783+
cur = cur.Next // 切换到下一个节点
784+
}
758785
}
759786

787+
760788
```
761789

762790
```go

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /