forked from Ada-C7/Linked-List
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedlist.go
More file actions
256 lines (215 loc) · 4.79 KB
/
linkedlist.go
File metadata and controls
256 lines (215 loc) · 4.79 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package linkedlist
import (
"errors"
"fmt"
)
// LinkedList stores the head Node of a linked list
type LinkedList struct {
head *Node
}
// Node stores the data and pointer to the next Node in a linked list
type Node struct {
data int
next *Node
}
// Insert inserts a new node with specified value into the linked list
func (ll *LinkedList) Insert(val int) {
newNode := Node{val, ll.head}
ll.head = &newNode
}
// InsertAscending insert a node into into a linked list in ascending order
func (ll *LinkedList) InsertAscending(n int) {
if ll.head == nil || n <= ll.head.data {
newNode := Node{n, ll.head}
ll.head = &newNode
return
}
current := ll.head
for current.next != nil {
if n <= current.next.data {
newNode := Node{n, current.next}
current.next = &newNode
return
}
current = current.next
}
current.next = &Node{n, nil}
}
// Visit returns a string of the values of a linked list and prints them
func (ll LinkedList) Visit() string {
dataStr := ""
current := ll.head
for current != nil {
dataStr += fmt.Sprintf("%d", current.data)
if current.next != nil {
dataStr += " "
}
fmt.Printf("%v ", current.data)
current = current.next
}
fmt.Println()
return dataStr
}
// Search returns true if the specified value is found in the linked list
// and false if not
func (ll LinkedList) Search(val int) bool {
current := ll.head
for current != nil {
if current.data == val {
return true
}
current = current.next
}
return false
}
// FindMax finds the maximum value in the linked list
func (ll LinkedList) FindMax() (int, error) {
if ll.head == nil {
return -1, errors.New("no maximum value found")
}
current := ll.head
max := ll.head.data
for current != nil {
if current.data > max {
max = current.data
}
current = current.next
}
return max, nil
}
// FindMin finds the minimum value in the linked list
func (ll LinkedList) FindMin() (int, error) {
if ll.head == nil {
return -1, errors.New("no minimum value found")
}
current := ll.head
min := ll.head.data
for current != nil {
if current.data < min {
min = current.data
}
current = current.next
}
return min, nil
}
// Length returns the number of nodes in the linked list
func (ll LinkedList) Length() int {
count := 0
current := ll.head
for current != nil {
count++
current = current.next
}
return count
}
// FindNthFromBeginning returns the value of nth node from the beginning
func (ll LinkedList) FindNthFromBeginning(n int) (int, error) {
count := 0
current := ll.head
for current != nil {
if count == n {
return current.data, nil
}
count++
current = current.next
}
return -1, fmt.Errorf("There is no element at index %v", n)
}
// FindNthFromEnd returns the value of the nth from the end node in the linked list
func (ll LinkedList) FindNthFromEnd(n int) (int, error) {
current := ll.head
for i := 0; i <= n; i++ {
if current == nil {
return -1, fmt.Errorf("There is no element %v from the end.", n)
}
current = current.next
}
nthBehind := ll.head
for current != nil {
current = current.next
nthBehind = nthBehind.next
}
return nthBehind.data, nil
}
// FindMiddleValue returns the value of the middle node in the linked list
func (ll LinkedList) FindMiddleValue() (int, error) {
if ll.head == nil {
return -1, errors.New("cannot find middle value")
}
middle := ll.head
end := ll.head.next
for end != nil {
if end.next != nil {
middle = middle.next
end = end.next.next
} else {
end = nil
}
}
return middle.data, nil
}
// Reverse reverses the nodes in the linked list
func (ll *LinkedList) Reverse() {
if ll.head == nil || ll.head.next == nil {
return
}
prev := ll.head
current := ll.head.next
prev.next = nil
for current != nil {
temp := current.next
current.next = prev
prev = current
current = temp
}
ll.head = prev
}
// Delete deletes the first node in a linked list that holds the specified value
func (ll *LinkedList) Delete(n int) {
if ll.head.data == n {
ll.head = ll.head.next
return
}
temp := ll.head
current := ll.head
for current != nil {
if current.data == n {
temp.next = current.next
return
}
temp = current
current = current.next
}
}
// CreateCycle creates a cycle in the linked list
func (ll *LinkedList) CreateCycle() error {
if ll.Length() == 0 {
return errors.New("cannot create a cycle with no nodes")
}
current := ll.head
for current.next != nil {
current = current.next
}
current.next = ll.head
return nil
}
// HasCycle returns true if the linked list has a cycle and false otherwise
func (ll LinkedList) HasCycle() bool {
if ll.head == nil {
return false
}
slow := ll.head
fast := ll.head.next
for fast != nil {
if fast.next != nil {
fast = fast.next.next
} else {
return false
}
slow = slow.next
if slow == fast {
return true
}
}
return false
}