-
Notifications
You must be signed in to change notification settings - Fork 48
Time - Yoyo #38
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
base: master
Are you sure you want to change the base?
Time - Yoyo #38
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,71 +18,148 @@ 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) | ||||||||||||||||||||||||||||||||||||||||||||||||
if @head.nil? | ||||||||||||||||||||||||||||||||||||||||||||||||
@head = new_node | ||||||||||||||||||||||||||||||||||||||||||||||||
else | ||||||||||||||||||||||||||||||||||||||||||||||||
new_node.next = @head | ||||||||||||||||||||||||||||||||||||||||||||||||
@head = new_node | ||||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+24
to
+30
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 can be simplified as:
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||
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(1) | ||||||||||||||||||||||||||||||||||||||||||||||||
# Space Complexity: O(1) | ||||||||||||||||||||||||||||||||||||||||||||||||
def search(value) | ||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+35
to
37
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. 👍 The time complexity here is O(n) |
||||||||||||||||||||||||||||||||||||||||||||||||
raise NotImplementedError | ||||||||||||||||||||||||||||||||||||||||||||||||
if @head.nil? | ||||||||||||||||||||||||||||||||||||||||||||||||
return false | ||||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||||
current = @head | ||||||||||||||||||||||||||||||||||||||||||||||||
until current.data == value || current.next.nil? | ||||||||||||||||||||||||||||||||||||||||||||||||
current = current.next | ||||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||||
return current.data == value | ||||||||||||||||||||||||||||||||||||||||||||||||
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
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 | ||||||||||||||||||||||||||||||||||||||||||||||||
if @head.nil? | ||||||||||||||||||||||||||||||||||||||||||||||||
return nil | ||||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||||
current = @head | ||||||||||||||||||||||||||||||||||||||||||||||||
max = @head.data | ||||||||||||||||||||||||||||||||||||||||||||||||
until current.next.nil? | ||||||||||||||||||||||||||||||||||||||||||||||||
current = current.next | ||||||||||||||||||||||||||||||||||||||||||||||||
if current.data > max | ||||||||||||||||||||||||||||||||||||||||||||||||
max = current.data | ||||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||||
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(n) | ||||||||||||||||||||||||||||||||||||||||||||||||
def find_min | ||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+67
to
69
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. 👍 , but the space complexity is O(1) because you're not making a new list here. |
||||||||||||||||||||||||||||||||||||||||||||||||
raise NotImplementedError | ||||||||||||||||||||||||||||||||||||||||||||||||
if @head.nil? | ||||||||||||||||||||||||||||||||||||||||||||||||
return nil | ||||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||||
current = @head | ||||||||||||||||||||||||||||||||||||||||||||||||
min = @head.data | ||||||||||||||||||||||||||||||||||||||||||||||||
until current.next.nil? | ||||||||||||||||||||||||||||||||||||||||||||||||
current = current.next | ||||||||||||||||||||||||||||||||||||||||||||||||
if current.data < min | ||||||||||||||||||||||||||||||||||||||||||||||||
min = current.data | ||||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||||
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
+86
to
88
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 | ||||||||||||||||||||||||||||||||||||||||||||||||
if @head.nil? | ||||||||||||||||||||||||||||||||||||||||||||||||
return 0 | ||||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||||
count = 1 | ||||||||||||||||||||||||||||||||||||||||||||||||
current = @head | ||||||||||||||||||||||||||||||||||||||||||||||||
until current.next.nil? | ||||||||||||||||||||||||||||||||||||||||||||||||
current = current.next | ||||||||||||||||||||||||||||||||||||||||||||||||
count += 1 | ||||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||||
return count | ||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+89
to
+98
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 can be simplified a bit.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||
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) | ||||||||||||||||||||||||||||||||||||||||||||||||
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. What if |
||||||||||||||||||||||||||||||||||||||||||||||||
raise NotImplementedError | ||||||||||||||||||||||||||||||||||||||||||||||||
if @head.nil? | ||||||||||||||||||||||||||||||||||||||||||||||||
return nil | ||||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||||
current = @head | ||||||||||||||||||||||||||||||||||||||||||||||||
count = 0 | ||||||||||||||||||||||||||||||||||||||||||||||||
until count == index || current.next.nil? | ||||||||||||||||||||||||||||||||||||||||||||||||
current = current.next | ||||||||||||||||||||||||||||||||||||||||||||||||
count += 1 | ||||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||||
return current.data | ||||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
# method to print all the values in the linked list | ||||||||||||||||||||||||||||||||||||||||||||||||
# Time Complexity: ? | ||||||||||||||||||||||||||||||||||||||||||||||||
# Space Complexity: ? | ||||||||||||||||||||||||||||||||||||||||||||||||
# Time Complexity: O(n) | ||||||||||||||||||||||||||||||||||||||||||||||||
# Space Complexity: O(n) | ||||||||||||||||||||||||||||||||||||||||||||||||
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. 👍 , but the space complexity is O(1) because you're not making a new list here. |
||||||||||||||||||||||||||||||||||||||||||||||||
raise NotImplementedError | ||||||||||||||||||||||||||||||||||||||||||||||||
if @head.nil? | ||||||||||||||||||||||||||||||||||||||||||||||||
return nil | ||||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||||
current = @head | ||||||||||||||||||||||||||||||||||||||||||||||||
until current.nil? | ||||||||||||||||||||||||||||||||||||||||||||||||
print current.data | ||||||||||||||||||||||||||||||||||||||||||||||||
current = current.next | ||||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
# method to delete the first node found with specified value | ||||||||||||||||||||||||||||||||||||||||||||||||
# Time Complexity: ? | ||||||||||||||||||||||||||||||||||||||||||||||||
# Space Complexity: ? | ||||||||||||||||||||||||||||||||||||||||||||||||
# Time Complexity: O(n) | ||||||||||||||||||||||||||||||||||||||||||||||||
# Space Complexity: O(n) | ||||||||||||||||||||||||||||||||||||||||||||||||
def delete(value) | ||||||||||||||||||||||||||||||||||||||||||||||||
raise NotImplementedError | ||||||||||||||||||||||||||||||||||||||||||||||||
if @head.nil? | ||||||||||||||||||||||||||||||||||||||||||||||||
return nil | ||||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||||
previous = @head | ||||||||||||||||||||||||||||||||||||||||||||||||
current = @head | ||||||||||||||||||||||||||||||||||||||||||||||||
until current.data == value || current.nil? | ||||||||||||||||||||||||||||||||||||||||||||||||
previous = current | ||||||||||||||||||||||||||||||||||||||||||||||||
current = current.next | ||||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||||
previous.next == current.next | ||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+137
to
+146
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. You also need a check here to see if
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||
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
+151
to
153
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 | ||||||||||||||||||||||||||||||||||||||||||||||||
if @head.nil? | ||||||||||||||||||||||||||||||||||||||||||||||||
return nil | ||||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||||
current = @head | ||||||||||||||||||||||||||||||||||||||||||||||||
until current.nil? | ||||||||||||||||||||||||||||||||||||||||||||||||
new_previous = current.next | ||||||||||||||||||||||||||||||||||||||||||||||||
new_current = new_previous.next | ||||||||||||||||||||||||||||||||||||||||||||||||
new_previous.next = current | ||||||||||||||||||||||||||||||||||||||||||||||||
current = new_current | ||||||||||||||||||||||||||||||||||||||||||||||||
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. Missing
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -115,25 +192,45 @@ def has_cycle | |||||||||||||||||||||||||||||||||||||||||||||||
# 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
+195
to
197
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 | ||||||||||||||||||||||||||||||||||||||||||||||||
if @head.nil? | ||||||||||||||||||||||||||||||||||||||||||||||||
return nil | ||||||||||||||||||||||||||||||||||||||||||||||||
else | ||||||||||||||||||||||||||||||||||||||||||||||||
return @head.data | ||||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||||
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
+206
to
208
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) | ||||||||||||||||||||||||||||||||||||||||||||||||
if @head.nil? | ||||||||||||||||||||||||||||||||||||||||||||||||
@head = new_node | ||||||||||||||||||||||||||||||||||||||||||||||||
else | ||||||||||||||||||||||||||||||||||||||||||||||||
current = @head | ||||||||||||||||||||||||||||||||||||||||||||||||
until current.next.nil? | ||||||||||||||||||||||||||||||||||||||||||||||||
current = current.next | ||||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||||
current.next = new_node | ||||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||||
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: ? | ||||||||||||||||||||||||||||||||||||||||||||||||
# Time Complexity: O(1) | ||||||||||||||||||||||||||||||||||||||||||||||||
# Space Complexity: O(1) | ||||||||||||||||||||||||||||||||||||||||||||||||
def get_last | ||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+223
to
225
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. 👍 , but the time complexity is O(n) since you're traveling to the end of the list. |
||||||||||||||||||||||||||||||||||||||||||||||||
raise NotImplementedError | ||||||||||||||||||||||||||||||||||||||||||||||||
if @head.nil? | ||||||||||||||||||||||||||||||||||||||||||||||||
return nil | ||||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||||
current = @head | ||||||||||||||||||||||||||||||||||||||||||||||||
while current.next != nil | ||||||||||||||||||||||||||||||||||||||||||||||||
current = current.next | ||||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||||
return current.data | ||||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
# method to insert a new node with specific data value, assuming the linked | ||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -158,4 +255,5 @@ def create_cycle | |||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
current.next = @head # make the last node link to first node | ||||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||||
end |
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.
👍