-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
707. Design Linked List.go
106 lines (97 loc) · 2.42 KB
/
707. Design Linked List.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
package leetcode
type MyLinkedList struct {
head *Node
}
type Node struct {
Val int
Next *Node
Prev *Node
}
/** 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 {
curr := this.head
for i := 0; i < index && curr != nil; i++ {
curr = curr.Next
}
if curr != nil {
return curr.Val
} else {
return -1
}
}
/** 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) {
node := &Node{Val: val}
node.Next = this.head
if this.head != nil {
this.head.Prev = node
}
this.head = node
}
/** Append a node of value val to the last element of the linked list. */
func (this *MyLinkedList) AddAtTail(val int) {
if this.head == nil {
this.AddAtHead(val)
return
}
node := &Node{Val: val}
curr := this.head
for curr != nil && curr.Next != nil {
curr = curr.Next
}
node.Prev = curr
curr.Next = node
}
/** 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 == 0 {
this.AddAtHead(val)
} else {
node := &Node{Val: val}
curr := this.head
for i := 0; i < index-1 && curr != nil; i++ {
curr = curr.Next
}
if curr != nil {
node.Next = curr.Next
node.Prev = curr
if node.Next != nil {
node.Next.Prev = node
}
curr.Next = node
}
}
}
/** Delete the index-th node in the linked list, if the index is valid. */
func (this *MyLinkedList) DeleteAtIndex(index int) {
if index == 0 {
this.head = this.head.Next
if this.head != nil {
this.head.Prev = nil
}
} else {
curr := this.head
for i := 0; i < index-1 && curr != nil; i++ {
curr = curr.Next
}
if curr != nil && curr.Next != nil {
curr.Next = curr.Next.Next
if curr.Next != nil {
curr.Next.Prev = curr
}
}
}
}
/**
* 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);
*/