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 7568254

Browse files
author
Shuo
authored
Merge pull request #529 from openset/develop
Add: Design Linked List
2 parents 7974e9d + 77cda35 commit 7568254

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,68 @@
11
package design_linked_list
2+
3+
import "container/list"
4+
5+
type MyLinkedList struct {
6+
data *list.List
7+
}
8+
9+
/** Initialize your data structure here. */
10+
func Constructor() MyLinkedList {
11+
return MyLinkedList{list.New()}
12+
}
13+
14+
/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
15+
func (this *MyLinkedList) Get(index int) int {
16+
if index < 0 || index >= this.data.Len() {
17+
return -1
18+
}
19+
node := this.data.Front()
20+
for i := 0; i < index; i++ {
21+
node = node.Next()
22+
}
23+
return node.Value.(int)
24+
}
25+
26+
/** 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. */
27+
func (this *MyLinkedList) AddAtHead(val int) {
28+
this.data.PushFront(val)
29+
}
30+
31+
/** Append a node of value val to the last element of the linked list. */
32+
func (this *MyLinkedList) AddAtTail(val int) {
33+
this.data.PushBack(val)
34+
}
35+
36+
/** 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. */
37+
func (this *MyLinkedList) AddAtIndex(index int, val int) {
38+
if index == this.data.Len() {
39+
this.AddAtTail(val)
40+
} else if index >= 0 && index < this.data.Len() {
41+
node := this.data.Front()
42+
for i := 0; i < index; i++ {
43+
node = node.Next()
44+
}
45+
this.data.InsertBefore(val, node)
46+
}
47+
}
48+
49+
/** Delete the index-th node in the linked list, if the index is valid. */
50+
func (this *MyLinkedList) DeleteAtIndex(index int) {
51+
if index >= 0 && index < this.data.Len() {
52+
node := this.data.Front()
53+
for i := 0; i < index; i++ {
54+
node = node.Next()
55+
}
56+
this.data.Remove(node)
57+
}
58+
}
59+
60+
/**
61+
* Your MyLinkedList object will be instantiated and called as such:
62+
* obj := Constructor();
63+
* param_1 := obj.Get(index);
64+
* obj.AddAtHead(val);
65+
* obj.AddAtTail(val);
66+
* obj.AddAtIndex(index,val);
67+
* obj.DeleteAtIndex(index);
68+
*/
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,18 @@
11
package design_linked_list
2+
3+
import "testing"
4+
5+
func TestConstructor(t *testing.T) {
6+
obj := Constructor()
7+
obj.AddAtHead(1)
8+
obj.AddAtTail(3)
9+
obj.AddAtIndex(1, 2) // linked list becomes 1->2->3
10+
output := obj.Get(1) == 2 // returns 2
11+
obj.DeleteAtIndex(1) // now the linked list is 1->3
12+
output = obj.Get(1) == 3 && output
13+
obj.AddAtIndex(2, 2) // linked list becomes 1->3->2
14+
output = obj.Get(5) == -1 && output
15+
if !output {
16+
t.Fatalf("output: %v, expected: %v", output, true)
17+
}
18+
}

0 commit comments

Comments
(0)

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