-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Linkedlist submission #36
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,97 +18,160 @@ def initialize | |
|
||
# 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: ? | ||
# Space Complexity: ? | ||
# Time Complexity: O(1) | ||
# Space Complexity: O(1) | ||
def add_first(value) | ||
raise NotImplementedError | ||
new_node = Node.new(value, nil) | ||
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: ? | ||
# Space Complexity: ? | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(1) | ||
def search(value) | ||
Comment on lines
+31
to
33
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
raise NotImplementedError | ||
current = @head | ||
|
||
while current !=nil | ||
if current.data == value | ||
return true | ||
end | ||
current = current.next | ||
end | ||
|
||
return false | ||
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 | ||
Comment on lines
+48
to
50
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
raise NotImplementedError | ||
return if @head == nil | ||
|
||
current = @head | ||
max = current.data | ||
|
||
while current.next != nil | ||
if max < current.next.data | ||
max = current.next.data | ||
end | ||
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: ? | ||
# Space Complexity: ? | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(1) | ||
def find_min | ||
Comment on lines
+69
to
71
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
raise NotImplementedError | ||
end | ||
return if @head == nil | ||
|
||
current = @head | ||
min = current.data | ||
|
||
# Additional Exercises | ||
# returns the value in the first node | ||
# returns nil if the list is empty | ||
# Time Complexity: ? | ||
# Space Complexity: ? | ||
def get_first | ||
raise NotImplementedError | ||
end | ||
while current.next != nil | ||
if current.next.data < min | ||
min = current.next.data | ||
end | ||
current = current.next | ||
end | ||
|
||
# method that inserts a given value as a new last node in the linked list | ||
# Time Complexity: ? | ||
# Space Complexity: ? | ||
def add_last(value) | ||
raise NotImplementedError | ||
return min | ||
end | ||
|
||
|
||
# method that returns the length of the singly linked list | ||
# Time Complexity: ? | ||
# Space Complexity: ? | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(1) | ||
def length | ||
Comment on lines
+89
to
91
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
raise NotImplementedError | ||
current = @head | ||
count = 0 | ||
while current !=nil | ||
current = current.next | ||
count += 1 | ||
end | ||
return count | ||
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: ? | ||
# Space Complexity: ? | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(1) | ||
def get_at_index(index) | ||
Comment on lines
+104
to
106
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
raise NotImplementedError | ||
current = @head | ||
count = 0 | ||
while current !=nil | ||
if index == count | ||
return current.data | ||
end | ||
current = current.next | ||
count += 1 | ||
end | ||
return nil | ||
end | ||
|
||
# method to print all the values in the linked list | ||
# Time Complexity: ? | ||
# Space Complexity: ? | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(1) | ||
def visit | ||
Comment on lines
+120
to
122
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
raise NotImplementedError | ||
return if @head == nil | ||
current = @head | ||
|
||
while current.next !=nil | ||
current = current.next | ||
print current.data | ||
end | ||
end | ||
|
||
# method to delete the first node found with specified value | ||
# Time Complexity: ? | ||
# Space Complexity: ? | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(1) | ||
def delete(value) | ||
Comment on lines
+133
to
135
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
raise NotImplementedError | ||
return 0 if @head == nil | ||
|
||
current = @head | ||
if current.data == value | ||
@head = current.next | ||
return | ||
end | ||
|
||
while current.next != nil | ||
if current.next.data == value | ||
current.next = current.next.next | ||
return | ||
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: ? | ||
# Space Complexity: ? | ||
# Time Complexity: O(n) | ||
# Space Complexity: O(1) | ||
def reverse | ||
Comment on lines
+156
to
158
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
raise NotImplementedError | ||
end | ||
return if @head == nil | ||
current = @head | ||
previous = nil | ||
|
||
while !current.nil? | ||
next_node = current.next | ||
current.next = previous | ||
previous = current | ||
current = next_node | ||
end | ||
|
||
@head = previous | ||
|
||
# method that returns the value of the last node in the linked list | ||
# returns nil if the linked list is empty | ||
# Time Complexity: ? | ||
# Space Complexity: ? | ||
def get_last | ||
raise NotImplementedError | ||
end | ||
|
||
|
||
|
||
## Advanced Exercises | ||
# returns the value at the middle element in the singly linked list | ||
# Time Complexity: ? | ||
|
@@ -134,6 +197,32 @@ def has_cycle | |
raise NotImplementedError | ||
end | ||
|
||
|
||
# Additional Exercises | ||
# returns the value in the first node | ||
# returns nil if the list is empty | ||
# Time Complexity: ? | ||
# Space Complexity: ? | ||
def get_first | ||
Comment on lines
+203
to
+206
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
return if @head == nil | ||
return @head.value | ||
end | ||
|
||
# method that inserts a given value as a new last node in the linked list | ||
# Time Complexity: ? | ||
# Space Complexity: ? | ||
def add_last(value) | ||
raise NotImplementedError | ||
end | ||
|
||
# method that returns the value of the last node in the linked list | ||
# returns nil if the linked list is empty | ||
# Time Complexity: ? | ||
# Space Complexity: ? | ||
def get_last | ||
raise NotImplementedError | ||
end | ||
|
||
# method to insert a new node with specific data value, assuming the linked | ||
# list is sorted in ascending order | ||
# Time Complexity: ? | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍