forked from AdaGold/linked-list
-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathlinked_list.rb
More file actions
296 lines (264 loc) · 7.85 KB
/
linked_list.rb
File metadata and controls
296 lines (264 loc) · 7.85 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# Defines a node in the singly linked list
class Node
attr_reader :data # allow external entities to read value but not write
attr_accessor :next # allow external entities to read or write next node
def initialize(value, next_node = nil)
@data = value
@next = next_node
end
end
# Defines the singly linked list
class LinkedList
def initialize
@head = nil # keep the head private. Not accessible outside this class
end
# method to add a new node with the specific data value in the linked list
# insert the new node at the beginning of the linked list
# Time Complexity: O(1)
# Space Complexity O(1)
def add_first(value)
new_node = Node.new(value)
new_node.next = @head
@head = new_node
end
# method to find if the linked list contains a node with specified value
# returns true if found, false otherwise
# Time Complexity: O(n)
# Space Complexity: O(1)
def search(value)
return false if @head.nil?
return true if @head.data = value
current = @head
until current.data.nil?
return true if current.data = value
current = current.next
end
end
# method to return the max value in the linked list
# returns the data value and not the node
# Time Complexity: O(n)
# Space Complexity: O(1)
def find_max
return nil if @head.nil?
current = @head
max = current.data
until current.nil?
max = current.data if current.data > max
current = current.next
end
return max
end
# method to return the min value in the linked list
# returns the data value and not the node
# Time Complexity: O(n)
# Space Complexity: O(1)
def find_min
return nil if @head.nil?
current = @head
min = current.data
until current.nil?
if current.data < min
min = current.data
end
current = current.next
end
return min
end
# method that returns the length of the singly linked list
# Time Complexity: O(n)
# Space Complexity: O(1)
def length
return 0 if @head.nil?
length = 0
current = @head
until current.nil?
current = current.next
length += 1
end
return length
end
# method that returns the value at a given index in the linked list
# index count starts at 0
# returns nil if there are fewer nodes in the linked list than the index value
# Time Complexity: O(n)
# Space Complexity: O(1)
def get_at_index(index)
return nil if length <= index
current = @head
count = 0
until current.nil?
return current.data if count == index
current = current.next
count += 1
end
end
# method to print all the values in the linked list
# Time Complexity: O(n)
# Space Complexity: O(1)
def visit
current = @head
until current.nil?
puts current.data
current = current.next
end
end
# method to delete the first node found with specified value
# Time Complexity: O(n) where n is the number of nodes
# Space Complexity: O(1)
def delete(value)
return if @head.nil?
current = @head
if current.data == value
@head = current.next
return
end
prev = current
until current.nil?
if current.data == value
prev.next = current.next
else
prev = current
end
current = current.next
end
end
# method to reverse the singly linked list
# note: the nodes should be moved and not just the values in the nodes
# Time Complexity: O(n) where n is the number of nodes
# Space Complexity: O(1)
def reverse
return nil if @head.nil?
prev = nil
current = @head
until current.nil?
temp = current.next
current.next = prev
prev = current
current = temp
end
@head = prev
end
## Advanced Exercises
# returns the value at the middle element in the singly linked list
# Time Complexity: O(n)
# Space Complexity: O(1)
def find_middle_value
return nil if @head.nil?
current = @head
if length % 2 == 0
return nil
else
middle_index = length / 2
end
index = 0
until current.nil?
return current.data if index == middle_index
current = current.next
index += 1
end
end
# find the nth node from the end and return its value
# assume indexing starts at 0 while counting to n
# Time Complexity: O(n)
# Space Complexity: O(1)
def find_nth_from_end(n)
return nil if @head.nil?
current = @head
index = 0
until current.nil?
if index == length - n - 1
return current.data
end
index += 1
current = current.next
end
end
# checks if the linked list has a cycle. A cycle exists if any node in the
# linked list links to a node already visited.
# returns true if a cycle is found, false otherwise.
# Time Complexity: O(n)
# Space Complexity: O(1)
def has_cycle
return nil if @head.nil?
slow_p = @head
fast_p = @head
while slow_p != nil && fast_p != nil and fast_p.next != nil
slow_p = slow_p.next
fast_p = fast_p.next.next
if slow_p == fast_p
return true
end
return false
end
end
# Additional Exercises
# returns the value in the first node
# returns nil if the list is empty
# Time Complexity: O(1)
# Space Complexity: O(1)
def get_first
return nil if @head.nil?
return @head.data
end
# method that inserts a given value as a new last node in the linked list
# Time Complexity: O(n) where n is the number of nodes
# Space Complexity: O(1)
def add_last(value)
new_node = Node.new(value)
if @head.nil?
@head = new_node
return
end
current = @head
until current.next.nil?
current = current.next
end
current.next = new_node
end
# method that returns the value of the last node in the linked list
# returns nil if the linked list is empty
# Time Complexity: O(n) where n is the number of nodes
# Space Complexity: O(1)
def get_last
return nil if @head.nil?
current = @head
until current.next.nil?
current = current.next
end
return current.data
end
# method to insert a new node with specific data value, assuming the linked
# list is sorted in ascending order
# Time Complexity: O(n)
# Space Complexity: O(1)
def insert_ascending(value)
new_node = Node.new(value)
@head = new_node if @head.nil?
current = @head
if current.data > new_node.data
temp = @head
@head = new_node
new_node.next = temp
end
until current.nil?
if current.data <= value && current.next.data > value
temp = current.next
current.next = new_node
new_node.next = temp
end
current = current.next
end
end
# Helper method for tests
# Creates a cycle in the linked list for testing purposes
# Assumes the linked list has at least one node
def create_cycle
return if @head == nil # don't do anything if the linked list is empty
# navigate to last node
current = @head
while current.next != nil
current = current.next
end
current.next = @head # make the last node link to first node
end
end