-
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
Earth - Kal #39
base: master
Are you sure you want to change the base?
Earth - Kal #39
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,95 +18,195 @@ 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 | ||||||||
@head = Node.new(value, @head) | ||||||||
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
+29
to
31
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_node = @head | ||||||||
|
||||||||
until current_node.nil? | ||||||||
return true if current_node.data == value | ||||||||
current_node = current_node.next | ||||||||
end | ||||||||
|
||||||||
return false | ||||||||
end | ||||||||
|
||||||||
# method to return the max value in the linked list | ||||||||
# returns the data value and not the node | ||||||||
def find_max | ||||||||
Comment on lines
42
to
44
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 nil if @head.nil? | ||||||||
|
||||||||
current_node = @head | ||||||||
max = 0 | ||||||||
until current_node.nil? | ||||||||
if current_node.data > max | ||||||||
max = current_node.data | ||||||||
end | ||||||||
current_node = current_node.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
+61
to
63
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 nil if @head.nil? | ||||||||
|
||||||||
current_node = @head | ||||||||
min = current_node.data | ||||||||
|
||||||||
until current_node.nil? | ||||||||
if current_node.data < min | ||||||||
min = current_node.data | ||||||||
end | ||||||||
current_node = current_node.next | ||||||||
end | ||||||||
|
||||||||
return min | ||||||||
end | ||||||||
|
||||||||
|
||||||||
# Additional Exercises | ||||||||
# returns the value in the first node | ||||||||
# returns nil if the list is empty | ||||||||
# Time Complexity: ? | ||||||||
# Space Complexity: ? | ||||||||
# Time Complexity: O(1) | ||||||||
# Space Complexity: O(1) | ||||||||
def get_first | ||||||||
Comment on lines
+83
to
85
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 nil if @head.nil? # I don't understand why this is not passing the test | ||||||||
|
||||||||
return @head.data | ||||||||
end | ||||||||
|
||||||||
# method that inserts a given value as a new last node in the linked list | ||||||||
# Time Complexity: ? | ||||||||
# Space Complexity: ? | ||||||||
# Time Complexity: O(n) | ||||||||
# Space Complexity: O(1) | ||||||||
def add_last(value) | ||||||||
Comment on lines
+92
to
94
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 | ||||||||
new_node = Node.new(value) | ||||||||
|
||||||||
current_node = @head | ||||||||
if current_node.nil? | ||||||||
return @head = new_node | ||||||||
end | ||||||||
|
||||||||
until current_node.next.nil? | ||||||||
current_node = current_node.next | ||||||||
end | ||||||||
|
||||||||
current_node.next = new_node | ||||||||
end | ||||||||
|
||||||||
# method that returns the length of the singly linked list | ||||||||
# Time Complexity: ? | ||||||||
# Space Complexity: ? | ||||||||
# Time Complexity: O(1) | ||||||||
# Space Complexity: O(1) | ||||||||
def length | ||||||||
Comment on lines
+110
to
112
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_node = @head | ||||||||
counter = 0 | ||||||||
until current_node.nil? | ||||||||
counter += 1 | ||||||||
current_node = current_node.next | ||||||||
end | ||||||||
|
||||||||
return counter | ||||||||
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
+126
to
128
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 | ||||||||
length = self.length | ||||||||
|
||||||||
return nil if index > length | ||||||||
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. This would fix the failing test
Suggested change
|
||||||||
|
||||||||
current_node = @head | ||||||||
|
||||||||
index.times do | ||||||||
current_node = current_node.next | ||||||||
end | ||||||||
|
||||||||
return current_node.data | ||||||||
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
+143
to
145
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_node = @head | ||||||||
|
||||||||
return nil if current_node.nil? | ||||||||
until current_node.nil? | ||||||||
puts current_node.data | ||||||||
current_node = current_node.next | ||||||||
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
+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 | ||||||||
return nil if @head.nil? | ||||||||
current_node = @head | ||||||||
previous = nil | ||||||||
|
||||||||
until current_node.nil? | ||||||||
if current_node.data == value | ||||||||
# previous.next = current.next | ||||||||
if previous == nil | ||||||||
@head = current_node.next | ||||||||
else | ||||||||
previous.next = current_node.next | ||||||||
end | ||||||||
end | ||||||||
previous = current_node | ||||||||
current_node = current_node.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
+179
to
181
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. 👍 Nicely done |
||||||||
raise NotImplementedError | ||||||||
return nil if @head.nil? | ||||||||
current_node = @head | ||||||||
previous = nil | ||||||||
|
||||||||
until current_node.next.nil? | ||||||||
temp = current_node.next | ||||||||
current_node.next = previous | ||||||||
previous = current_node | ||||||||
current_node = temp | ||||||||
end | ||||||||
|
||||||||
current_node.next = previous | ||||||||
@head = current_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: ? | ||||||||
# Space Complexity: ? | ||||||||
def get_last | ||||||||
Comment on lines
199
to
201
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. 👍 , I assume now you know the time/space complexity |
||||||||
raise NotImplementedError | ||||||||
return nil if @head.nil? | ||||||||
|
||||||||
current_node = @head | ||||||||
until current_node.next == nil | ||||||||
current_node = current_node.next | ||||||||
end | ||||||||
|
||||||||
return current_node.data | ||||||||
end | ||||||||
|
||||||||
## Advanced Exercises | ||||||||
|
@@ -122,7 +222,22 @@ def find_middle_value | |||||||
# Time Complexity: ? | ||||||||
# Space Complexity: ? | ||||||||
def find_nth_from_end(n) | ||||||||
Comment on lines
222
to
224
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 nil if @head.nil? | ||||||||
|
||||||||
current_node = @head | ||||||||
n_ahead = @head | ||||||||
|
||||||||
n.times do | ||||||||
return if n_ahead.next.nil? | ||||||||
n_ahead = n_ahead.next | ||||||||
end | ||||||||
|
||||||||
until n_ahead.next.nil? | ||||||||
current_node = current_node.next | ||||||||
n_ahead = n_ahead.next | ||||||||
end | ||||||||
|
||||||||
return current_node.data | ||||||||
end | ||||||||
|
||||||||
# checks if the linked list has a cycle. A cycle exists if any node in the | ||||||||
|
@@ -131,15 +246,40 @@ def find_nth_from_end(n) | |||||||
# Time Complexity: ? | ||||||||
# Space Complexity: ? | ||||||||
def has_cycle | ||||||||
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 nil if @head.nil? | ||||||||
|
||||||||
fast = @head | ||||||||
slow = @head | ||||||||
|
||||||||
until fast.nil? || fast.next.nil? | ||||||||
fast = fast.next.next | ||||||||
slow = slow.next | ||||||||
return true if fast == slow | ||||||||
end | ||||||||
|
||||||||
return false | ||||||||
end | ||||||||
|
||||||||
# method to insert a new node with specific data value, assuming the linked | ||||||||
# list is sorted in ascending order | ||||||||
# Time Complexity: ? | ||||||||
# Space Complexity: ? | ||||||||
def insert_ascending(value) | ||||||||
raise NotImplementedError | ||||||||
return nil if @head.nil? | ||||||||
|
||||||||
current_node = @head | ||||||||
Comment on lines
+268
to
+270
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. I think here you'll need to check if head is nil, then insert first. |
||||||||
|
||||||||
until current_node.nil? | ||||||||
if value > current_node.data && value <= current_node.next.data | ||||||||
new_node = Node.new(value, current_node.next) | ||||||||
current_node.next = new_node | ||||||||
return | ||||||||
else | ||||||||
current_node = current_node.next | ||||||||
end | ||||||||
end | ||||||||
|
||||||||
return current_node = Node.new(value) | ||||||||
end | ||||||||
|
||||||||
# Helper method for tests | ||||||||
|
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.
👍