-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdesignlist.go
134 lines (119 loc) · 2.71 KB
/
designlist.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package designsinglylist
type MyLinkedList struct {
val interface{}
len int
head *MyLinkedList
tail *MyLinkedList
next *MyLinkedList
}
/** Initialize your data structure here. */
func Constructor() MyLinkedList {
return MyLinkedList{}
}
/** 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 index < 0 || index >= this.len {
return -1
}
tmp := new(MyLinkedList)
tmp = this.head
for i := 0; i < index; i++ {
tmp = tmp.next
}
v, ok := tmp.val.(int)
if !ok {
panic("get val error")
}
return v
}
/** 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. */
func (this *MyLinkedList) AddAtHead(val int) {
if this.head == nil {
this.head = this
this.tail = this
this.val = val
this.len++
return
}
this.len++
this.head = &MyLinkedList{
val: val,
next: this.head,
}
}
/** Append a node of value val to the last element of the linked list. */
func (this *MyLinkedList) AddAtTail(val int) {
if this.tail == nil {
this.head = this
this.tail = this
this.val = val
this.len++
return
}
this.len++
tmp := &MyLinkedList{}
tmp.val = val
this.tail.next = tmp
this.tail = tmp
}
/** 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. */
func (this *MyLinkedList) AddAtIndex(index int, val int) {
if index > this.len || index < 0 {
return
}
if index == this.len {
this.AddAtTail(val)
return
}
if index == 0 {
this.AddAtHead(val)
return
}
this.len++
tmp := new(MyLinkedList)
tmp = this.head
for i := 0; i < index-1; i++ {
tmp = tmp.next
}
tmpcur := &MyLinkedList{}
tmpcur.val = val
tmpcur.next = tmp.next
tmp.next = tmpcur
}
/** 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.len {
return
}
if this.len == 1 {
this.val = nil
this.len--
this.head = nil
this.tail = nil
this.next = nil
return
}
// find previous node which node need to delete
tmp := new(MyLinkedList)
tmp = this.head
for i := 0; i < index-1; i++ {
tmp = tmp.next
}
if index == this.len-1 {
this.len--
this.tail = tmp
tmp.next = nil
return
}
this.len--
tmp.next = tmp.next.next
}
/**
* Your MyLinkedList object will be instantiated and called as such:
* obj := Constructor();
* param_1 := obj.Get(index);
* obj.AddAtHead(val);
* obj.AddAtTail(val);
* obj.AddAtIndex(index,val);
* obj.DeleteAtIndex(index);
*/