8000 feat: 707 链表的基本操作 · chenshiwei-io/leetcode@675dc56 · GitHub
[go: up one dir, main page]

Skip to content

Commit 675dc56

Browse files
author
陈世伟
committed
feat: 707 链表的基本操作
1 parent 887b2f4 commit 675dc56

File tree

3 files changed

+132
-1
lines changed

3 files changed

+132
-1
lines changed

leetcode/0019/remove-nth-form-end.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ func RemoveNthFromEnd1(head *ListNode, n int) *ListNode {
5252

5353
func RemoveNthFromEnd2(head *ListNode, n int) *ListNode {
5454

55+
// 防止删除头节点 做哨兵
5556
newListNode := new(ListNode)
5657
newListNode.Next = head
5758

@@ -60,7 +61,8 @@ func RemoveNthFromEnd2(head *ListNode, n int) *ListNode {
6061
var count int
6162
for i != nil {
6263
i = i.Next
63-
if count < (n + 1) {
64+
// 需要指向的是删除的上一个节点
65+
if count <= (n) {
6466
count++
6567
} else {
6668
j = j.Next
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package _07_设计链表
2+
3+
type ListNode struct {
4+
Next *ListNode
5+
Val int
6+
}
7+
8+
type MyLinkedList struct {
9+
head *ListNode
10+
size int
11+
}
12+
13+
/** Initialize your data structure here. */
14+
func Constructor() MyLinkedList {
15+
sentinel := &ListNode{Val: 0}
16+
return MyLinkedList{head: sentinel, size: 0}
17+
}
18+
19+
/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
20+
func (this *MyLinkedList) Get(index int) int {
21+
if index < 0 || index >= this.size {
22+
return -1
23+
}
24+
cur := this.head
25+
26+
var c int
27+
for cur.Next != nil && c <= index {
28+
c++
29+
cur = cur.Next
30+
}
31+
32+
return cur.Val
33+
34+
}
35+
36+
/** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */
37+
func (this *MyLinkedList) AddAtHead(val int) {
38+
newNode := &ListNode{
39+
Val: val,
40+
}
41+
newNode.Next = this.head.Next
42+
this.head.Next = newNode
43+
this.size++
44+
return
45+
}
46+
47+
/** Append a node of value val to the last element of the linked list. */
48+
func (this *MyLinkedList) AddAtTail(val int) {
49+
newListNode := &ListNode{Val: val}
50+
cur := this.head
51+
for cur.Next != nil {
52+
cur = cur.Next
53+
}
54+
cur.Next = newListNode
55+
this.size++
56+
}
57+
58+
/** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */
59+
func (this *MyLinkedList) AddAtIndex(index int, val int) {
60+
if index > this.size {
61+
return
62+
}
63+
newListNode := &ListNode{Val: val}
64+
cur := this.head
65+
var c int
66+
for cur.Next != nil && c < index {
67+
cur = cur.Next
68+
c++
69+
}
70+
newListNode.Next = cur.Next
71+
cur.Next = newListNode
72+
this.size++
73+
}
74+
75+
/** Delete the index-th node in the linked list, if the index is valid. */
76+
func (this *MyLinkedList) DeleteAtIndex(index int) {
77+
if index < 0 || index >= this.size {
78+
return
79+
}
80+
cur := this.head
81+
var c int
82+
for cur.Next != nil && c < index {
83+
cur = cur.Next
84+
c++
85+
}
86+
cur.Next = cur.Next.Next
87+
this.size--
88+
}
89+
90+
/**
91+
* Your MyLinkedList object will be instantiated and called as such:
92+
* obj := Constructor();
93+
* param_1 := obj.Get(index);
94+
* obj.AddAtHead(val);
95+
* obj.AddAtTail(val);
96+
* obj.AddAtIndex(index,val);
97+
* obj.DeleteAtIndex(index);
98+
*/
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package _07_设计链表
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func TestConstructor(t *testing.T) {
9+
linkedList := Constructor()
10+
linkedList.AddAtHead(1)
11+
printList(linkedList.head.Next)
12+
linkedList.AddAtTail(3)
13+
printList(linkedList.head.Next)
14+
linkedList.AddAtIndex(1, 2) //链表变为1-> 2-> 3
15+
printList(linkedList.head.Next)
16+
fmt.Println("返回2", linkedList.Get(1)) //返回2
17+
printList(linkedList.head.Next)
18+
linkedList.DeleteAtIndex(1) //现在链表是1-> 3
19+
printList(linkedList.head.Next)
20+
fmt.Println("返回3", linkedList.Get(1)) //返回3
21+
}
22+
23+
func printList(node *ListNode) {
24+
if node == nil {
25+
fmt.Println()
26+
return
27+
}
28+
fmt.Printf("%+v =>", node)
29+
30+
printList(node.Next)
31+
}

0 commit comments

Comments
 (0)
0